Skip to content

Commit

Permalink
TinyRoute support subdirectorys now, and also the build-in HTTP serve…
Browse files Browse the repository at this point in the history
…r of PHP.
  • Loading branch information
johnlui committed Mar 12, 2015
1 parent 7c3d4fd commit f22682e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
22 changes: 18 additions & 4 deletions TinyRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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, '/'));
}
}

0 comments on commit f22682e

Please sign in to comment.