diff --git a/Route.php b/Route.php index cc45729..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, @@ -60,27 +67,30 @@ class Route{ $route['expression'] = $route['expression'].'$'; // echo $route['expression'].'
'; - + // Check path match if(preg_match('#'.$route['expression'].'#'.($case_matters ? '':'i'),$path,$matches)){ $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..cc8957f 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/
  • @@ -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 +?>