How to use routes well?

I’m new to Slim, and I’m creating a really simple app with a “/” route. But Slim returns me a 404. THe only way to make it work is to append the whole path on the server : “/PHP/Lumy%203/tests/”. Then it works.

Shouldn’t Slim take care of the relative base path on the server or am I missing something?

Can you provide your code sample and the directory structure?

There is just one index.php. Located in the “/PHP/Lumy 3/tests” directory (as seen from the server).
The app is based on Lumy, which is a wrapper for Slim, allowing me to use an ArrayAccess oriented container. All Lumy’s method calls are mapped to Slim.

$app = new Lumy\App();

$app['twig'] = $app->service(function($app) {
  $loader = new Twig_Loader_Filesystem('.');
  return new Twig_Environment($loader, ['debug', true]);

$app->get('/PHP/Lumy%203/tests/', function($request, $response) {
	return $this['twig']->render('index.twig');


Just in case, I want to be able to do :

$app->get('/', function($request, $response) {
	return $this['twig']->render('index.twig');

Just as a side note; The default slim container (pimple) already supports ArrayAccess.

But have you tried the example you gave?

$app->get('/', function($request, $response) {
	return $this['twig']->render('index.twig');

With a fresh installation of Slim within a subdirectory called slim the home path / works fine for me


require 'vendor/autoload.php';

$app = new Slim\App();

$app->get('/', function($request, $response) {
    return $response->write('Test response');


Can you test if the above index.php works as expected? Just to try if Slim itself runs fine withing the sub directory.

Yes, Pimple supports ArrayAccess. But the Slim\App class does not implement it directly. And I prefer the Chernozem container because it has a lot of stuff too.

I’ve tested a cleaner example with just Slim and the behavior is the same.

$app = new Slim\App();

$container = $app->getContainer();
$container['twig'] = function($app) {
  $loader = new Twig_Loader_Filesystem('.');
  return new Twig_Environment($loader, ['debug', true]);

$app->get('/PHP/Lumy%203/tests/', function($request, $response) {
	return $this['twig']->render('index.twig');


And the problem should not come from the server itself, it works as expected with another framework. I think I’ll need to dig in the code ^^

The path is defined here, in Slim/Http/Uri.php, line 205 :

$requestUri = parse_url('' . $env->get('REQUEST_URI'), PHP_URL_PATH);

Can you tell me what $requestUri is equal to, in your case?

edit : the value is used to set $virtualPath, which can be altered in the lines below 205 (which is not my case)

Your example still does not contain a route for the home path. Please try my example.

I tried it, and it returns a 404

I cannot test anything atm. as Im at work.

If you are debugging by dumping variables in the Uri class; $basePath should end up containing the part of the path from the server to the index. /PHP/Lumy%203/tests in your case (off the top of my head)

If you cannot figure it out please dump the contents of your environment.

In fact, $basePath is empty, and $virtualPath equals to /PHP/Lumy%203/tests/. Here is the environment dump :

object(Slim\Http\Environment)#20 (1) {
  ["data":protected] => array(37) {
    ["REDIRECT_STATUS"] => string(3) "200"
    ["HTTP_HOST"] => string(9) "localhost"
    ["HTTP_CONNECTION"] => string(10) "keep-alive"
    ["HTTP_CACHE_CONTROL"] => string(9) "max-age=0"
    ["HTTP_UPGRADE_INSECURE_REQUESTS"] => string(1) "1"
    ["HTTP_USER_AGENT"] => string(126) "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41"
    ["HTTP_ACCEPT"] => string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
    ["HTTP_ACCEPT_ENCODING"] => string(25) "gzip, deflate, lzma, sdch"
    ["HTTP_ACCEPT_LANGUAGE"] => string(35) "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4"
    ["PATH"] => string(798) "C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\binaries\php\php_runningversion;c:\program files\graphicsmagick-1.3.23-q16;C:\Program Files (x86)\PC Connectivity Solution\;C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\php\php_runningversion;C:\Python34\;C:\Python34\Scripts;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Git\cmd;C:\ProgramData\ComposerSetup\bin;C:\Program Files\php;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\php;C:\Program Files\nodejs\;C:\Program Files (x86)\Brackets\command;"
    ["SystemRoot"] => string(10) "C:\Windows"
    ["COMSPEC"] => string(27) "C:\Windows\system32\cmd.exe"
    ["PATHEXT"] => string(57) ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY"
    ["WINDIR"] => string(10) "C:\Windows"
    ["SERVER_SIGNATURE"] => string(0) ""
    ["SERVER_SOFTWARE"] => string(31) "Apache/2.4.18 (Win64) PHP/7.0.2"
    ["SERVER_NAME"] => string(9) "localhost"
    ["SERVER_ADDR"] => string(3) "::1"
    ["SERVER_PORT"] => string(2) "80"
    ["REMOTE_ADDR"] => string(3) "::1"
    ["DOCUMENT_ROOT"] => string(18) "C:/Apache24/htdocs"
    ["REQUEST_SCHEME"] => string(4) "http"
    ["CONTEXT_PREFIX"] => string(0) ""
    ["CONTEXT_DOCUMENT_ROOT"] => string(18) "C:/Apache24/htdocs"
    ["SERVER_ADMIN"] => string(17) ""
    ["SCRIPT_FILENAME"] => string(45) "C:/Apache24/htdocs/PHP/Lumy 3/tests/index.php"
    ["REMOTE_PORT"] => string(5) "56245"
    ["REDIRECT_URL"] => string(18) "/PHP/Lumy 3/tests/"
    ["GATEWAY_INTERFACE"] => string(7) "CGI/1.1"
    ["SERVER_PROTOCOL"] => string(8) "HTTP/1.1"
    ["REQUEST_METHOD"] => string(3) "GET"
    ["QUERY_STRING"] => string(0) ""
    ["REQUEST_URI"] => string(20) "/PHP/Lumy%203/tests/"
    ["SCRIPT_NAME"] => string(27) "/PHP/Lumy 3/tests/index.php"
    ["PHP_SELF"] => string(27) "/PHP/Lumy 3/tests/index.php"
    ["REQUEST_TIME_FLOAT"] => float(1470229540.691)
    ["REQUEST_TIME"] => int(1470229540)

SCRIPT_NAME and REQUEST_URI are used to determine the basePath

If I look at your example:

 'REQUEST_URI' => "/PHP/Lumy%203/tests/",
 'SCRIPT_NAME' => "/PHP/Lumy 3/tests/index.php",

One is url encoded, the other is not. Guess that is where the problem lies.

Can you try to remove the space and see if that solves the problem?

That was the problem! Thanks a lot!

Do you think I should report this as a bug?

Well, I don’t think its very common to have a space in the path. Nobody ever reported the problem before, but you could if you like.

I will look if this is easily solvable later tonight anyway :wink:

Created a PR with the fix :wink:


Anyway, thanks a lot for your help!