From 338c77594d64cfcab160d2da7911299c309163fc Mon Sep 17 00:00:00 2001 From: Sander A Date: Thu, 31 Jan 2019 03:26:38 +0200 Subject: [PATCH 1/4] Remove trailing slash from routes Remove trailing slash from route (unless it's the root slash) before matching against routes. This ensures that both "/events" and "/events/" are matched with the route "/events" --- Route.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Route.php b/Route.php index dfd91ec..5495cee 100644 --- a/Route.php +++ b/Route.php @@ -27,8 +27,8 @@ class Route{ // Parse current url $parsed_url = parse_url($_SERVER['REQUEST_URI']);//Parse Uri - if(isset($parsed_url['path'])){ - $path = $parsed_url['path']; + if(isset($parsed_url['path']) && $parsed_url['path'] != '/'){ + $path = rtrim($parsed_url['path'], '/'); }else{ $path = '/'; } From 33c54c8511b98f1d53bfc69cca2fdf3311214c02 Mon Sep 17 00:00:00 2001 From: Sander A Date: Fri, 22 Feb 2019 01:06:38 +0200 Subject: [PATCH 2/4] Added option to define routes with multiple allowed methods --- Route.php | 23 +++++++++++++---------- index.php | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Route.php b/Route.php index b529fb9..e47ea3a 100644 --- a/Route.php +++ b/Route.php @@ -66,21 +66,24 @@ class Route{ $path_match_found = true; - // Check method match - if(strtolower($method) == strtolower($route['method'])){ + // Cast allowed method to array if it's not one already, then run through all methods + foreach ((array)$route['method'] as $allowedMethod) { + // Check method match + if(strtolower($method) == strtolower($allowedMethod)){ - array_shift($matches);// Always remove first element. This contains the whole string + array_shift($matches);// Always remove first element. This contains the whole string - if($basepath!=''&&$basepath!='/'){ - array_shift($matches);// Remove basepath - } + if($basepath!=''&&$basepath!='/'){ + array_shift($matches);// Remove basepath + } - call_user_func_array($route['function'], $matches); + call_user_func_array($route['function'], $matches); - $route_match_found = true; + $route_match_found = true; - // Do not check other routes - break; + // Do not check other routes + break; + } } } } diff --git a/index.php b/index.php index 4cb7711..797e612 100644 --- a/index.php +++ b/index.php @@ -11,6 +11,7 @@ function navi () {
  • foo 5 bar
  • long route example
  • contact form
  • +
  • get+post example
  • test.html
  • aTrailingSlashDoesNotMatters
  • aTrailingSlashDoesNotMatters/
  • @@ -43,7 +44,7 @@ Route::add('/test.html',function(){ echo 'Hello from test.html'; }); -// Post route example +// Get route example Route::add('/contact-form',function(){ navi(); echo '
    '; @@ -56,6 +57,17 @@ Route::add('/contact-form',function(){ print_r($_POST); },'post'); +// Get and Post route example +Route::add('/get-post-sample',function(){ + navi(); + echo 'You can GET this page and also POST this form back to it'; + echo '
    '; + if(isset($_POST["input"])){ + echo 'I also received a POST with this data:
    '; + print_r($_POST); + } +},['get','post']); + // Route with regexp parameter // Be aware that (.*) will match / (slash) too. For example: /user/foo/bar/edit // Also users could inject mysql-code or other untrusted data if you use (.*) @@ -127,4 +139,4 @@ Route::run('/'); // Enable case sensitive mode and trailing slashes by setting both to true // Route::run('/', true, true); -?> \ No newline at end of file +?> From 4322382046f3e3fed6cdd7718fe099c702789224 Mon Sep 17 00:00:00 2001 From: Sander A Date: Fri, 22 Feb 2019 01:08:36 +0200 Subject: [PATCH 3/4] Added Phpdoc to the Route::add method For easier understanding of accepted types and better code validation support --- Route.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Route.php b/Route.php index e47ea3a..c01c8e2 100644 --- a/Route.php +++ b/Route.php @@ -6,6 +6,13 @@ class Route{ private static $pathNotFound = null; private static $methodNotAllowed = null; + /** + * Function used to add a new route + * @param string $expression Route string or expression + * @param callable $function Function to call when route with allowed method is found + * @param string|array $method Either a string of allowed method or an array with string values + * + */ public static function add($expression, $function, $method = 'get'){ array_push(self::$routes,Array( 'expression' => $expression, From 3788078fe115cec1a8a1e6cbf648b870de9a5b45 Mon Sep 17 00:00:00 2001 From: Sander A Date: Fri, 22 Feb 2019 01:10:47 +0200 Subject: [PATCH 4/4] Renamed sample back to original Misunderstood reference --- index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.php b/index.php index 797e612..cc8957f 100644 --- a/index.php +++ b/index.php @@ -44,7 +44,7 @@ Route::add('/test.html',function(){ echo 'Hello from test.html'; }); -// Get route example +// Post route example Route::add('/contact-form',function(){ navi(); echo '
    ';