The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Using Slim in REST Microservice Architecture

Marius's Avatar


22 Oct, 2014 03:49 PM


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?


  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:

    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: - main instance - sub instances - sub instances

    folder layout:
    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

    or you can grab my slim here, you just need the slim.php and environment.php:

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


? 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