Merge pull request #5 from sanderaido/master
Added option to define routes with multiple allowed methods
This commit is contained in:
commit
7153d369c1
30
Route.php
30
Route.php
|
@ -6,6 +6,13 @@ class Route{
|
||||||
private static $pathNotFound = null;
|
private static $pathNotFound = null;
|
||||||
private static $methodNotAllowed = 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'){
|
public static function add($expression, $function, $method = 'get'){
|
||||||
array_push(self::$routes,Array(
|
array_push(self::$routes,Array(
|
||||||
'expression' => $expression,
|
'expression' => $expression,
|
||||||
|
@ -66,21 +73,24 @@ class Route{
|
||||||
|
|
||||||
$path_match_found = true;
|
$path_match_found = true;
|
||||||
|
|
||||||
// Check method match
|
// Cast allowed method to array if it's not one already, then run through all methods
|
||||||
if(strtolower($method) == strtolower($route['method'])){
|
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!='/'){
|
if($basepath!=''&&$basepath!='/'){
|
||||||
array_shift($matches);// Remove 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
|
// Do not check other routes
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
12
index.php
12
index.php
|
@ -11,6 +11,7 @@ function navi () {
|
||||||
<li><a href="/foo/5/bar">foo 5 bar</a></li>
|
<li><a href="/foo/5/bar">foo 5 bar</a></li>
|
||||||
<li><a href="/foo/bar/foo/bar">long route example</a></li>
|
<li><a href="/foo/bar/foo/bar">long route example</a></li>
|
||||||
<li><a href="/contact-form">contact form</a></li>
|
<li><a href="/contact-form">contact form</a></li>
|
||||||
|
<li><a href="/get-post-sample">get+post example</a></li>
|
||||||
<li><a href="/test.html">test.html</a></li>
|
<li><a href="/test.html">test.html</a></li>
|
||||||
<li><a href="/aTrailingSlashDoesNotMatters">aTrailingSlashDoesNotMatters</a></li>
|
<li><a href="/aTrailingSlashDoesNotMatters">aTrailingSlashDoesNotMatters</a></li>
|
||||||
<li><a href="/aTrailingSlashDoesNotMatters/">aTrailingSlashDoesNotMatters/</a></li>
|
<li><a href="/aTrailingSlashDoesNotMatters/">aTrailingSlashDoesNotMatters/</a></li>
|
||||||
|
@ -56,6 +57,17 @@ Route::add('/contact-form',function(){
|
||||||
print_r($_POST);
|
print_r($_POST);
|
||||||
},'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 '<form method="post"><input type="text" name="input" /><input type="submit" value="send" /></form>';
|
||||||
|
if(isset($_POST["input"])){
|
||||||
|
echo 'I also received a POST with this data:<br/>';
|
||||||
|
print_r($_POST);
|
||||||
|
}
|
||||||
|
},['get','post']);
|
||||||
|
|
||||||
// Route with regexp parameter
|
// Route with regexp parameter
|
||||||
// Be aware that (.*) will match / (slash) too. For example: /user/foo/bar/edit
|
// 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 (.*)
|
// Also users could inject mysql-code or other untrusted data if you use (.*)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user