Added more example rules and comments.
This commit is contained in:
parent
730130e369
commit
782141717a
10
.htaccess
10
.htaccess
|
@ -1,10 +1,14 @@
|
||||||
DirectoryIndex index.php
|
DirectoryIndex index.php
|
||||||
|
|
||||||
|
#enable apache rewrite engine
|
||||||
RewriteEngine on
|
RewriteEngine on
|
||||||
|
|
||||||
|
#set your rewrite base
|
||||||
RewriteBase /
|
RewriteBase /
|
||||||
|
|
||||||
|
#Deliver the folder or file if it exists on the server directly
|
||||||
RewriteCond %{REQUEST_FILENAME} !-f
|
RewriteCond %{REQUEST_FILENAME} !-f
|
||||||
RewriteCond %{REQUEST_FILENAME} !-d
|
RewriteCond %{REQUEST_FILENAME} !-d
|
||||||
|
|
||||||
|
#Push every request to index.php
|
||||||
RewriteRule ^(.*)$ index.php [QSA]
|
RewriteRule ^(.*)$ index.php [QSA]
|
|
@ -8,7 +8,7 @@ class Route{
|
||||||
|
|
||||||
public static function init(){
|
public static function init(){
|
||||||
|
|
||||||
$parsed_url = parse_url($_SERVER['REQUEST_URI']);//URI zerlegen
|
$parsed_url = parse_url($_SERVER['REQUEST_URI']);//Parse Uri
|
||||||
|
|
||||||
if(isset($parsed_url['path'])){
|
if(isset($parsed_url['path'])){
|
||||||
self::$path = trim($parsed_url['path'],'/');
|
self::$path = trim($parsed_url['path'],'/');
|
||||||
|
|
34
index.php
34
index.php
|
@ -10,12 +10,18 @@ Config::set('basepath','');
|
||||||
//init routing
|
//init routing
|
||||||
Route::init();
|
Route::init();
|
||||||
|
|
||||||
//base route
|
//base route (startpage)
|
||||||
Route::add('',function(){
|
Route::add('',function(){
|
||||||
//Do something
|
//Do something
|
||||||
echo 'Welcome :-)';
|
echo 'Welcome :-)';
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//base route
|
||||||
|
Route::add('index.php',function(){
|
||||||
|
//Do something
|
||||||
|
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
|
||||||
|
@ -25,14 +31,34 @@ Route::add('test.html',function(){
|
||||||
//complex route with parameter
|
//complex route with parameter
|
||||||
Route::add('user/(.*)/edit',function($id){
|
Route::add('user/(.*)/edit',function($id){
|
||||||
//Do something
|
//Do something
|
||||||
echo 'Edit user with id '.$id;
|
echo 'Edit user with id '.$id.'<br/>';
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//accept only numbers as the second parameter. Other chars will result in a 404
|
||||||
|
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(){
|
||||||
|
//Do something
|
||||||
|
echo 'hehe :-)<br/>';
|
||||||
|
});
|
||||||
|
|
||||||
|
//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
|
||||||
|
echo 'You have entered: '.$var1.' / '.$var2.' / '.$var3.' / '.$var4.'<br/>';
|
||||||
|
});
|
||||||
|
|
||||||
|
//Add a 404 Not found Route
|
||||||
Route::add404(function($url){
|
Route::add404(function($url){
|
||||||
|
|
||||||
//Send 404 Header
|
//Send 404 Header
|
||||||
header("HTTP/1.0 404 Not Found");
|
header("HTTP/1.0 404 Not Found");
|
||||||
echo '404 :-(';
|
echo '404 :-(<br/>';
|
||||||
|
echo $url.' not found!';
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user