From 5bbd61d112b7cac1944a503e766030677d492c6b Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 31 Jan 2019 09:24:54 +0100 Subject: [PATCH 1/7] Added docker test setup --- README.md | 15 +++++++++++++-- docker/image/Dockerfile | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 docker/image/Dockerfile diff --git a/README.md b/README.md index e70b77b..ec8efbc 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,19 @@ Do not forget to edit the basepath in .htaccess too if you are on Apache2. In or * 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 -## Test setup -There is a little Vagrant test setup. Just run ```vagrant up``` to spin up a Apache2 Webserver on Ubuntu. Then navigate to http://router.local after adding the machine IP to your hosts file. +## Test setup with docker +I have created a little docker test setup. + +1. Build the image: docker build -t simplephprouter docker/image + +2. Spin up a container + * On Linux / Mac or Windows Powershell use: ```docker run -d -p 80:80 -v $(pwd):/var/www/html --name simplephprouter simplephprouter``` + * On Windows CMD use ```docker run -d -p 80:80 -v %cd%:/var/www/html --name simplephprouter simplephprouter``` + +3. Open your browser and navigate to http://localhost + +## Test setup with vagrant (not longer maintained) +There is a little Vagrant test setup. Just run ```vagrant up``` to spin up a Apache2 Webserver on Ubuntu. Then navigate to http://router.local after adding the machine IP to your hosts file. This test setup is not longer maintained and will probably break in future. Use the docker test setup instead. ## Todo * Create demo configuration for nginx diff --git a/docker/image/Dockerfile b/docker/image/Dockerfile new file mode 100644 index 0000000..fe78a2f --- /dev/null +++ b/docker/image/Dockerfile @@ -0,0 +1,3 @@ +FROM php:7.2-apache + +RUN a2enmod rewrite From 6255a05061ea728144298478c9bffb312b7ce6f0 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 31 Jan 2019 10:17:40 +0100 Subject: [PATCH 2/7] Moved the navigation to a function to avoid the 'headers already sent' problem --- index.php | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/index.php b/index.php index 8909a37..516f424 100644 --- a/index.php +++ b/index.php @@ -1,44 +1,59 @@ - -Navigation: - - + Navigation: + + '; },'get'); // Post route example Route::add('/contact-form',function(){ + navi(); echo 'Hey! The form has been sent:
'; print_r($_POST); },'post'); @@ -48,38 +63,45 @@ Route::add('/contact-form',function(){ // Also users could inject mysql-code or other untrusted data if you use (.*) // You should better use a saver expression like /user/([0-9]*)/edit or /user/([A-Za-z]*)/edit Route::add('/user/(.*)/edit',function($id){ + navi(); echo 'Edit user with id '.$id.'
'; }); // Accept only numbers as parameter. Other characters will result in a 404 error Route::add('/foo/([0-9]*)/bar',function($var1){ + navi(); echo $var1.' is a great number!'; }); // Crazy route with parameters Route::add('/(.*)/(.*)/(.*)/(.*)',function($var1,$var2,$var3,$var4){ + navi(); echo 'This is the first match: '.$var1.' / '.$var2.' / '.$var3.' / '.$var4.'
'; }); // Long route example // This route gets never triggered because the route before matches too Route::add('/foo/bar/foo/bar',function(){ + navi(); echo 'This is the second match
'; }); // 405 test Route::add('/this-route-is-defined',function(){ + navi(); echo 'You need to patch this route to see this content'; },'patch'); // Add a 404 not found route Route::pathNotFound(function($path){ + navi(); echo 'Error 404 :-(
'; echo 'The requested path "'.$path.'" was not found!'; }); // Add a 405 method not allowed route Route::methodNotAllowed(function($path, $method){ + navi(); echo 'Error 405 :-(
'; echo 'The requested path "'.$path.'" exists. But the request method "'.$method.'" is not allowed on this path!'; }); From 80bfe096b826152b131b43ff87e027f71a9a466b Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 31 Jan 2019 10:54:07 +0100 Subject: [PATCH 3/7] Added handling for case and trailing slashes --- README.md | 5 +++++ Route.php | 16 ++++++++++------ index.php | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ec8efbc..c0dfa67 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Route.php b/Route.php index dfd91ec..cc45729 100644 --- a/Route.php +++ b/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'].'
'; - - // 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; diff --git a/index.php b/index.php index 516f424..fa45240 100644 --- a/index.php +++ b/index.php @@ -12,6 +12,10 @@ function navi () {
  • long route example
  • contact form
  • test.html
  • +
  • aTrailingSlashDoesNotMatters
  • +
  • aTrailingSlashDoesNotMatters/
  • +
  • theCaseDoesNotMatters
  • +
  • thecasedoesnotmatters
  • 404 Test
  • 405 Test
  • @@ -86,6 +90,18 @@ Route::add('/foo/bar/foo/bar',function(){ echo 'This is the second match
    '; }); +// Trailing slash example +Route::add('/aTrailingSlashDoesNotMatters',function(){ + navi(); + echo 'a trailing slash does not matters
    '; +}); + +// Case example +Route::add('/theCaseDoesNotMatters',function(){ + navi(); + echo 'the case does not matters
    '; +}); + // 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); + ?> \ No newline at end of file From 46cd9641bb5bea5e53eabe6bac73b252953fb822 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 31 Jan 2019 11:02:57 +0100 Subject: [PATCH 4/7] Deleted old route test --- index.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/index.php b/index.php index fa45240..4cb7711 100644 --- a/index.php +++ b/index.php @@ -25,12 +25,6 @@ function navi () { // Include router class include('Route.php'); -// Add navigation route (startpage) -Route::add('/',function(){ - navi(); - echo 'Welcome :-)'; -}); - // Add base route (startpage) Route::add('/',function(){ navi(); From 05596c4048eaefa2e2993d235baec072f9100eb0 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 31 Jan 2019 11:04:03 +0100 Subject: [PATCH 5/7] Corrected spelling --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c0dfa67..3617463 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,8 @@ By default the router will ignore trailing slashes. Set the third parameter to t * 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 -## Test setup with docker -I have created a little docker test setup. +## Test setup with Docker +I have created a little Docker test setup. 1. Build the image: docker build -t simplephprouter docker/image From bd2e4aa9515676ca2fe2b25a9ab8a3524011276b Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 31 Jan 2019 11:04:29 +0100 Subject: [PATCH 6/7] Corrected spelling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3617463..338e86d 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ I have created a little Docker test setup. 3. Open your browser and navigate to http://localhost -## Test setup with vagrant (not longer maintained) +## Test setup with Vagrant (not longer maintained) There is a little Vagrant test setup. Just run ```vagrant up``` to spin up a Apache2 Webserver on Ubuntu. Then navigate to http://router.local after adding the machine IP to your hosts file. This test setup is not longer maintained and will probably break in future. Use the docker test setup instead. ## Todo From 16ef364730f5147bde36e1684eb56ea18337b40c Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 31 Jan 2019 11:08:37 +0100 Subject: [PATCH 7/7] Corrected spelling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 338e86d..d0c77a8 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ By default the router will ignore trailing slashes. Set the third parameter to t ## Test setup with Docker I have created a little Docker test setup. -1. Build the image: docker build -t simplephprouter docker/image +1. Build the image: ```docker build -t simplephprouter docker/image``` 2. Spin up a container * On Linux / Mac or Windows Powershell use: ```docker run -d -p 80:80 -v $(pwd):/var/www/html --name simplephprouter simplephprouter```