I’m creating my first php restful web serivce using Slim 3.0 and I am having some trouble with the routing when I have an internal redirect. I’d like to setup my web service so that it can:
- be deployed to any folder without requiring configuration changes
- be accessed through a published url that may not match the physical location on the web server
As a test I have setup the following scenario…
The .htaccess file in the server root contains:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteRule ^(/)?$ /subfolder/index.html [L]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1 [L]
The web service is located in /subfolder/api and it’s .htaccess file contains:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
In index.php there is a single routing handler for hello/{name}.
My expectation was that I would be able to use a url such as /api/hello/everyone and for that to be rewritten to /subfolder/api/hello/everyone which would then be rewritten to /subfolder/api/index.php. The url rewriting does work as expected but the Slim routing fails and I receive a 404 Not Found.
To avoid the error I can:
- Access the web service by defining the physical location in the request url (i.e. use /subfolder/api/hello/everybody instead)
- Modify index.php so that the routing includes the location of the web service /api/hello/{name}
- Modify createFromEnvironment() in Slim/Http/Uri.php so that $requestUri is initialised with REDIRECT_URL rather than REQUEST_URI.
I’m not keen on solution 1 and 2 above, can anoyone offer any alternative solutions?
Would you consider this a bug in the Slim Framework?
Thanks in advance for any help on this matter.