Better readme and improved comments

This commit is contained in:
steampixel 2018-03-12 16:49:55 +01:00
parent f3d7866b37
commit 0d9829796f
4 changed files with 57 additions and 43 deletions

View File

@ -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]

View File

@ -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.'<br/>';
});
```
## Something dows not work?
Dont forget to set the basepath in your index.php and .htaccess file.

View File

@ -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'].'<br/>';
//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
}

View File

@ -1,72 +1,73 @@
<?PHP
//include
// Include needed files
include('Config.php');
include('Route.php');
//configure basepath
//If your script lives in the web root folder use a / , leave it empty or do not define this config
// Configure basepath
// If your script lives in the web root folder use a / , leave it empty or do not define this config
Config::set('basepath','/');
//If your script lives in a subfolder for example you can use the following example
//Config::set('basepath','/api/v1');
//init routing
// If your script lives in a subfolder you can use the following example
// Config::set('basepath','/api/v1');
// Init routing
Route::init();
//base route (startpage)
// Base route (startpage)
Route::add('/',function(){
//Do something
// Do something
echo 'Welcome :-)';
});
//base route
// Another base route example
Route::add('/index.php',function(){
//Do something
// Do something
echo 'You are not realy on index.php ;-)';
});
//simple route
// Simple route
Route::add('/test.html',function(){
//Do something
// Do something
echo 'Hello from test.html';
});
//complex route with parameter
//be aware that (.*) will trigger on / too for example: /user/foo/bar/edit
//also users could inject mysql-code if you use (.*)
//you should better use a saver expression like /user/([0-9]*)/edit or /user/([A-Za-z]*)/edit
// Complex route with parameter
// Be aware that (.*) will match / too. For example: /user/foo/bar/edit
// Also users could inject mysql-code or other unchecked 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){
//Do something
// Do something
echo 'Edit user with id '.$id.'<br/>';
});
//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 :-)<br/>';
});
//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.'<br/>';
});
//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 :-(<br/>';
echo $url.' not found!';
});
// Check if any of the defined routes will match and execute them
Route::run();