diff --git a/README.md b/README.md index ead6897..30b798c 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ require: { ```php use TinyLara\TinyRoute\TinyRoute as Route; -Route::get('', 'HomeController@home'); +Route::get('/', 'HomeController@home'); // GET Route::get('foo', function() { diff --git a/TinyRoute.php b/TinyRoute.php index 9c8391e..6d5f7b2 100644 --- a/TinyRoute.php +++ b/TinyRoute.php @@ -31,8 +31,7 @@ class TinyRoute { */ public static function __callstatic($method, $params) { - - $uri = dirname($_SERVER['PHP_SELF']).$params[0]; + $uri = $params[0]; $callback = $params[1]; if ( $method == 'any' ) { @@ -71,7 +70,7 @@ public static function error($callback) */ public static function dispatch($after=null) { - $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); + $uri = self::detect_uri(); $method = $_SERVER['REQUEST_METHOD']; $searches = array_keys(static::$patterns); @@ -163,7 +162,6 @@ public static function dispatch($after=null) } } - // run the error callback if the route was not found if ($found_route == false) { if (!self::$error_callback) { @@ -175,4 +173,20 @@ public static function dispatch($after=null) call_user_func(self::$error_callback); } } + + // detect true URI, inspired by CodeIgniter 2 + private static function detect_uri() + { + $uri = $_SERVER['REQUEST_URI']; + if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) { + $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME'])); + } elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) { + $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); + } + if ($uri == '/' || empty($uri)) { + return '/'; + } + $uri = parse_url($uri, PHP_URL_PATH); + return str_replace(array('//', '../'), '/', trim($uri, '/')); + } } \ No newline at end of file