From 8699df00602a6715e3f2ef4907e45c89b0e0f931 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 25 Oct 2017 08:25:13 +0200 Subject: [PATCH] Added leading slash to routes --- Route.php | 6 +++--- index.php | 28 ++++++++++++++-------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Route.php b/Route.php index 4d6b49f..135eb92 100644 --- a/Route.php +++ b/Route.php @@ -9,11 +9,11 @@ class Route{ public static function init(){ $parsed_url = parse_url($_SERVER['REQUEST_URI']);//Parse Uri - + if(isset($parsed_url['path'])){ - self::$path = trim($parsed_url['path'],'/'); + self::$path = $parsed_url['path']; }else{ - self::$path = ''; + self::$path = '/'; } } diff --git a/index.php b/index.php index af52936..813d5ef 100644 --- a/index.php +++ b/index.php @@ -3,63 +3,63 @@ //include include('Config.php'); include('Route.php'); - + //config Config::set('basepath',''); - + //init routing Route::init(); //base route (startpage) -Route::add('',function(){ +Route::add('/',function(){ //Do something echo 'Welcome :-)'; }); //base route -Route::add('index.php',function(){ +Route::add('/index.php',function(){ //Do something echo 'You are not realy on index.php ;-)'; }); //simple route -Route::add('test.html',function(){ +Route::add('/test.html',function(){ //Do something echo 'Hello from test.html'; }); - + //complex route with parameter -Route::add('user/(.*)/edit',function($id){ +Route::add('/user/(.*)/edit',function($id){ //Do something echo 'Edit user with id '.$id.'
'; }); //accept only numbers as the second parameter. Other chars will result in a 404 -Route::add('foo/([0-9]*)/bar',function($var1){ +Route::add('/foo/([0-9]*)/bar',function($var1){ //Do something echo $var1.' is a great number!'; }); //long route -Route::add('foo/bar/foo/bar',function(){ +Route::add('/foo/bar/foo/bar',function(){ //Do something echo 'hehe :-)
'; }); //crazy route with parameters (Will be triggered on the route pattern above too because it matches too) -Route::add('(.*)/(.*)/(.*)/(.*)',function($var1,$var2,$var3,$var4){ +Route::add('/(.*)/(.*)/(.*)/(.*)',function($var1,$var2,$var3,$var4){ //Do something echo 'You have entered: '.$var1.' / '.$var2.' / '.$var3.' / '.$var4.'
'; }); //Add a 404 Not found Route Route::add404(function($url){ - + //Send 404 Header header("HTTP/1.0 404 Not Found"); echo '404 :-(
'; echo $url.' not found!'; - + }); - -Route::run(); \ No newline at end of file + +Route::run();