Better readme and improved comments
This commit is contained in:
parent
f3d7866b37
commit
0d9829796f
|
@ -6,7 +6,7 @@ RewriteEngine on
|
||||||
# set your rewrite base
|
# set your rewrite base
|
||||||
RewriteBase /
|
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} !-f
|
||||||
RewriteCond %{REQUEST_FILENAME} !-d
|
RewriteCond %{REQUEST_FILENAME} !-d
|
||||||
|
|
||||||
|
|
27
README.md
27
README.md
|
@ -1,7 +1,20 @@
|
||||||
This is a simple example that shows a simple PHP routing system.
|
# Simple PHP Router
|
||||||
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.
|
Hey! This is a simple class that can handel the whole url routing for 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.
|
It utilizes RegExp and PHPs anonymous functions to create a lightweight and fast routing system.
|
||||||
The handler is the second argument of the add method.
|
|
||||||
Dont forget to set the basepath in index.php and .htaccess file.
|
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.
|
||||||
|
|
|
@ -52,7 +52,7 @@ class Route{
|
||||||
|
|
||||||
//echo $route['expression'].'<br/>';
|
//echo $route['expression'].'<br/>';
|
||||||
|
|
||||||
//check match
|
// Check match
|
||||||
if(preg_match('#'.$route['expression'].'#',self::$path,$matches)){
|
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
|
||||||
|
|
33
index.php
33
index.php
|
@ -1,65 +1,65 @@
|
||||||
<?PHP
|
<?PHP
|
||||||
|
|
||||||
//include
|
// Include needed files
|
||||||
include('Config.php');
|
include('Config.php');
|
||||||
include('Route.php');
|
include('Route.php');
|
||||||
|
|
||||||
//configure basepath
|
// Configure basepath
|
||||||
|
|
||||||
// If your script lives in the web root folder use a / , leave it empty or do not define this config
|
// If your script lives in the web root folder use a / , leave it empty or do not define this config
|
||||||
Config::set('basepath','/');
|
Config::set('basepath','/');
|
||||||
//If your script lives in a subfolder for example you can use the following example
|
|
||||||
|
// If your script lives in a subfolder you can use the following example
|
||||||
// Config::set('basepath','/api/v1');
|
// Config::set('basepath','/api/v1');
|
||||||
|
|
||||||
//init routing
|
// Init routing
|
||||||
Route::init();
|
Route::init();
|
||||||
|
|
||||||
//base route (startpage)
|
// Base route (startpage)
|
||||||
Route::add('/',function(){
|
Route::add('/',function(){
|
||||||
// Do something
|
// Do something
|
||||||
echo 'Welcome :-)';
|
echo 'Welcome :-)';
|
||||||
});
|
});
|
||||||
|
|
||||||
//base route
|
// Another base route example
|
||||||
Route::add('/index.php',function(){
|
Route::add('/index.php',function(){
|
||||||
// Do something
|
// Do something
|
||||||
echo 'You are not realy on index.php ;-)';
|
echo 'You are not realy on index.php ;-)';
|
||||||
});
|
});
|
||||||
|
|
||||||
//simple route
|
// Simple route
|
||||||
Route::add('/test.html',function(){
|
Route::add('/test.html',function(){
|
||||||
// Do something
|
// Do something
|
||||||
echo 'Hello from test.html';
|
echo 'Hello from test.html';
|
||||||
});
|
});
|
||||||
|
|
||||||
//complex route with parameter
|
// Complex route with parameter
|
||||||
//be aware that (.*) will trigger on / too for example: /user/foo/bar/edit
|
// Be aware that (.*) will match / too. For example: /user/foo/bar/edit
|
||||||
//also users could inject mysql-code if you use (.*)
|
// 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
|
// You should better use a saver expression like /user/([0-9]*)/edit or /user/([A-Za-z]*)/edit
|
||||||
Route::add('/user/(.*)/edit',function($id){
|
Route::add('/user/(.*)/edit',function($id){
|
||||||
// Do something
|
// Do something
|
||||||
echo 'Edit user with id '.$id.'<br/>';
|
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){
|
Route::add('/foo/([0-9]*)/bar',function($var1){
|
||||||
// Do something
|
// Do something
|
||||||
echo $var1.' is a great number!';
|
echo $var1.' is a great number!';
|
||||||
});
|
});
|
||||||
|
|
||||||
//long route
|
// Long route example
|
||||||
Route::add('/foo/bar/foo/bar',function(){
|
Route::add('/foo/bar/foo/bar',function(){
|
||||||
// Do something
|
// Do something
|
||||||
echo 'hehe :-)<br/>';
|
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){
|
Route::add('/(.*)/(.*)/(.*)/(.*)',function($var1,$var2,$var3,$var4){
|
||||||
// Do something
|
// Do something
|
||||||
echo 'You have entered: '.$var1.' / '.$var2.' / '.$var3.' / '.$var4.'<br/>';
|
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){
|
Route::add404(function($url){
|
||||||
|
|
||||||
// Send 404 Header
|
// Send 404 Header
|
||||||
|
@ -69,4 +69,5 @@ Route::add404(function($url){
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Check if any of the defined routes will match and execute them
|
||||||
Route::run();
|
Route::run();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user