diff --git a/.htaccess b/.htaccess index b3df3bb..a491805 100644 --- a/.htaccess +++ b/.htaccess @@ -1,14 +1,14 @@ DirectoryIndex index.php -#enable apache rewrite engine +# enable apache rewrite engine RewriteEngine on -#set your rewrite base +# set your rewrite base RewriteBase / -#Deliver the folder or file if it exists on the server directly +# Deliver the folder or file directly if it exists on the server RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d -#Push every request to index.php +# Push every request to index.php RewriteRule ^(.*)$ index.php [QSA] \ No newline at end of file diff --git a/README.md b/README.md index c4ef414..0572594 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,20 @@ -This is a simple example that shows a simple PHP routing system. -This is not for production use but for people who want to understand how such a system works. -Take a look in the index.php file. You can use the Route::add method to add a new route to your project. -The first argument takes the URL. -You can use Regexp in there to parse variables directly out of the URL und push them to the handler method. -The handler is the second argument of the add method. -Dont forget to set the basepath in index.php and .htaccess file. +# Simple PHP Router + +Hey! This is a simple class that can handel the whole url routing for your project. + +It utilizes RegExp and PHPs anonymous functions to create a lightweight and fast routing system. + +Take a look in the index.php file. As you can see the ```Route::add()``` method is used to add new routes to your project. +The first argument takes the path segment. You can also use RegExp in there to parse out variables. +All matches will be pushed to the handler method. The handler is the second argument of the add function. + +Example: +``` +Route::add('/user/(.*)/edit',function($id){ + //Do something + echo 'Edit user with id '.$id.'
'; +}); +``` + +## Something dows not work? +Dont forget to set the basepath in your index.php and .htaccess file. diff --git a/Route.php b/Route.php index bb629e3..acac07d 100644 --- a/Route.php +++ b/Route.php @@ -39,27 +39,27 @@ class Route{ foreach(self::$routes as $route){ - //Add basepath to matching string + // Add basepath to matching string if(Config::get('basepath')&&Config::get('basepath')!=''&&Config::get('basepath')!='/'){ $route['expression'] = '('.Config::get('basepath').')'.$route['expression']; } - //Add 'find string start' automatically + // Add 'find string start' automatically $route['expression'] = '^'.$route['expression']; - //Add 'find string end' automatically + // Add 'find string end' automatically $route['expression'] = $route['expression'].'$'; //echo $route['expression'].'
'; - //check match + // Check match if(preg_match('#'.$route['expression'].'#',self::$path,$matches)){ - 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(Config::get('basepath')&&Config::get('basepath')!=''&&Config::get('basepath')!='/'){ - array_shift($matches);//Remove Basepath + array_shift($matches);// Remove Basepath } diff --git a/index.php b/index.php index 72381d7..07c2141 100644 --- a/index.php +++ b/index.php @@ -1,72 +1,73 @@ '; }); -//accept only numbers as the second parameter. Other chars will result in a 404 +// Accept only numbers as the second parameter. Other characters will result in a 404 Route::add('/foo/([0-9]*)/bar',function($var1){ - //Do something + // Do something echo $var1.' is a great number!'; }); -//long route +// Long route example Route::add('/foo/bar/foo/bar',function(){ - //Do something + // Do something echo 'hehe :-)
'; }); -//crazy route with parameters (Will be triggered on the route pattern above too because it matches too) +// Crazy route with parameters (Will be triggered on the route pattern above too because it matches too) Route::add('/(.*)/(.*)/(.*)/(.*)',function($var1,$var2,$var3,$var4){ - //Do something + // Do something echo 'You have entered: '.$var1.' / '.$var2.' / '.$var3.' / '.$var4.'
'; }); -//Add a 404 Not found Route +// Add a 404 not found route Route::add404(function($url){ - //Send 404 Header + // Send 404 Header header("HTTP/1.0 404 Not Found"); echo '404 :-(
'; echo $url.' not found!'; }); +// Check if any of the defined routes will match and execute them Route::run();