The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Using Slim in REST Microservice Architecture

Marius's Avatar

Marius

22 Oct, 2014 03:49 PM

Hi,

I'm trying to use Slim as a Platform for some REST microservices. The concept is really simple, you separate a big service into smaller ones. However I have encountered a problem with mod_rewrite and routing on Apache, which I can't solve.
(RewriteBase /approot - not working either)

/approot contains .htaccess Each RewriteRule is a path to a microservice.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^area(.*)$ api/v1/area.php [QSA,L]
RewriteRule ^user(.*)$ api/v2/user.php [QSA,L]
Apache Root is in /htdocs
Application Root containing the single page application (index.html) is in /htdocs/approot

The api script for the first microservice is located in: /htdocs/approot/api/v1/area.php
The area.php contains routes starting with /area.
Every time I try to access the http://localhost/approot/area I get 404 Error from Slim.

I have read some solutions in this forum and tried for a few hours to get this to work, trying different routes and different rewrite options and traces (httpd.conf : LogLevel alert rewrite:trace3). At least, I could verify that rewrite does what it should.

So far, the only possibility to make it work is to put area.php as api_area.php into /htdocs/approot, which basically eliminates the idea of api versioning.

Is it possible to provide a solutioin to this dilemma other than to make /approot apache server root directory?

Thx!

  1. 1 Posted by CTS_AE on 04 Dec, 2014 09:36 AM

    CTS_AE's Avatar

    I've always had issues getting Slim to run under sub directories,
    especially when rewriting, it required the slim routes to contain the current folder in them

    I'm not 100% sure this applies to what you're trying to accomplish but this is what I ended up doing:
    https://github.com/codeguy/Slim/pull/941
    https://github.com/codeguy/Slim/pull/941/files

    Slim has code to chop off the extra routes, but if it doesn't match the request with the actual folder being ran in, then it doesn't remove that extra routing and requires you to either add the base route to all the routes, or you can implement this little hack I put in which allows you to specify the base route.

    I'm running on Nginx and I think my vhost file is a bit hacky but it works, if it would help you I could post more about my set up as I believe we were doing nearly the same things.
    For instance I have:
    http://litestack.tk/ - main instance
    http://litestack.tk/staging/insomnia/ - sub instances
    http://litestack.tk/staging/LoL/ - sub instances

    folder layout: http://puu.sh/dgxac/8b250cfd0d.png
    I ended up having the sub container folders have a public and a private folder, anything from that route runs through public folder

  2. 2 Posted by CTS_AE on 04 Dec, 2014 11:06 AM

    CTS_AE's Avatar

    Just realized after trying to use this in another project that I had a typo that just lead me to a bug, this fixes that issue after applying the other changes
    https://github.com/ctsstc/Slim/commit/9041845c4114f73037272cf802fa4a25eb643367

    or you can grab my slim here, you just need the slim.php and environment.php:
    https://github.com/ctsstc/Slim/tree/9041845c4114f73037272cf802fa4a25eb643367/Slim

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac