Added handling for case and trailing slashes
This commit is contained in:
parent
6255a05061
commit
80bfe096b8
|
@ -30,6 +30,11 @@ If your script lives in a subfolder e.g. /api/v1 set this basepath in your run m
|
|||
|
||||
Do not forget to edit the basepath in .htaccess too if you are on Apache2. In order to run the test files correctly inside a basepath you should also adjust the navigation links inside the index.php.
|
||||
|
||||
## Enable case sensitive routes and trailing slashes
|
||||
The second and third parameters of ```Route::run('/', false, false);``` are both set to false by default.
|
||||
You can enable case sensitive mode by setting the second parameter to true.
|
||||
By default the router will ignore trailing slashes. Set the third parameter to true to avoid this.
|
||||
|
||||
## Something does not work?
|
||||
* Dont forget to set the correct basepath as argument in your run method and in your .htaccess file.
|
||||
* Enable mod_rewrite in your Apache2 settings
|
||||
|
|
16
Route.php
16
Route.php
|
@ -22,13 +22,17 @@ class Route{
|
|||
self::$methodNotAllowed = $function;
|
||||
}
|
||||
|
||||
public static function run($basepath = '/'){
|
||||
public static function run($basepath = '/', $case_matters = false, $trailing_slash_matters = false){
|
||||
|
||||
// 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'] != '/'){
|
||||
if($trailing_slash_matters){
|
||||
$path = $parsed_url['path'];
|
||||
}else{
|
||||
$path = rtrim($parsed_url['path'], '/');
|
||||
}
|
||||
}else{
|
||||
$path = '/';
|
||||
}
|
||||
|
@ -56,9 +60,9 @@ class Route{
|
|||
$route['expression'] = $route['expression'].'$';
|
||||
|
||||
// echo $route['expression'].'<br/>';
|
||||
|
||||
// Check path match
|
||||
if(preg_match('#'.$route['expression'].'#',$path,$matches)){
|
||||
|
||||
// Check path match
|
||||
if(preg_match('#'.$route['expression'].'#'.($case_matters ? '':'i'),$path,$matches)){
|
||||
|
||||
$path_match_found = true;
|
||||
|
||||
|
|
19
index.php
19
index.php
|
@ -12,6 +12,10 @@ function navi () {
|
|||
<li><a href="/foo/bar/foo/bar">long route example</a></li>
|
||||
<li><a href="/contact-form">contact form</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="/theCaseDoesNotMatters">theCaseDoesNotMatters</a></li>
|
||||
<li><a href="/thecasedoesnotmatters">thecasedoesnotmatters</a></li>
|
||||
<li><a href="/this-route-is-not-defined">404 Test</a></li>
|
||||
<li><a href="/this-route-is-defined">405 Test</a></li>
|
||||
</ul>
|
||||
|
@ -86,6 +90,18 @@ Route::add('/foo/bar/foo/bar',function(){
|
|||
echo 'This is the second match <br/>';
|
||||
});
|
||||
|
||||
// Trailing slash example
|
||||
Route::add('/aTrailingSlashDoesNotMatters',function(){
|
||||
navi();
|
||||
echo 'a trailing slash does not matters<br/>';
|
||||
});
|
||||
|
||||
// Case example
|
||||
Route::add('/theCaseDoesNotMatters',function(){
|
||||
navi();
|
||||
echo 'the case does not matters<br/>';
|
||||
});
|
||||
|
||||
// 405 test
|
||||
Route::add('/this-route-is-defined',function(){
|
||||
navi();
|
||||
|
@ -114,4 +130,7 @@ Route::run('/');
|
|||
// Do not forget to edit the basepath in .htaccess if you are on apache
|
||||
// Route::run('/api/v1');
|
||||
|
||||
// Enable case sensitive mode and trailing slashes by setting both to true
|
||||
// Route::run('/', true, true);
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user