anandkunal / ToroPHP

Toro is a PHP router for developing RESTful web applications and APIs.
http://toroweb.org
MIT License
1.17k stars 173 forks source link

Default route fails if index.php is in a subdirectory #78

Open codazoda opened 10 years ago

codazoda commented 10 years ago

If I access the index.php that includes Toro.php from a URL with no path it works fine. Here's an example:

mytest.com/

If, however, I access an index.php file that is inside of a directory or two, it fails. Here's an example of that (my index is in /var/www/some/directory/index.php).

mytest.com/some/directory/

What I've found is that the last test in Toro.php that looks at the $_SERVER['REQUEST_URI'] is the problem. That variable returns "/some/directory/" in the case of my script being setup inside a directory. As a result the "/" route fails to work, but the others do work. Here's the failing route code.

Toro::serve(array(
    "/" => "Help",
    "/test" => "Test"
));

If I change the code to the following, it works.

Toro::serve(array(
    "/some/directory/" => "Help",
    "/test" => "Test"
));

By removing the last for $_SERVER['REQUEST_URI']) the problem goes away (because '/' is the default $path_info value).

So, my question is, what does that last $_SERVER['REQUEST_URI'] add that I'm missing here? Should it be removed or put in as an option? If so, I'm happy to make these changes and submit a pull request.

gyaaniguy commented 10 years ago

Hello. I can confirm this issue as well. removing the last else block (18 line) resolves it, but not sure if it won't cause trouble is some other situation...

sijad commented 10 years ago

I have this problem too!

VeeeneX commented 10 years ago

@sijad @kutchbhi You have several options how to fix it I'm using this: Toro.php

    $uri = @explode('/', $_SERVER['REQUEST_URI'])[2]; //Here you need to find your dir or use GET
    $path_info = ((!empty($uri)) ? $path_info = '/'.$uri : $path_info = '/');

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    IndexIgnore */*
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) index.php [L,QSA]
</IfModule>