initial commit
This commit is contained in:
commit
730130e369
10
.htaccess
Normal file
10
.htaccess
Normal file
|
@ -0,0 +1,10 @@
|
|||
DirectoryIndex index.php
|
||||
|
||||
RewriteEngine on
|
||||
|
||||
RewriteBase /
|
||||
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteCond %{REQUEST_FILENAME} !-d
|
||||
|
||||
RewriteRule ^(.*)$ index.php [QSA]
|
17
Config.php
Normal file
17
Config.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?PHP
|
||||
|
||||
class Config{
|
||||
|
||||
private static $registry = Array();
|
||||
|
||||
public static function set($key,$value){
|
||||
self::$registry[$key] = $value;
|
||||
}
|
||||
|
||||
public static function get($key){
|
||||
if(array_key_exists($key,self::$registry)){
|
||||
return self::$registry[$key];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
87
Route.php
Normal file
87
Route.php
Normal file
|
@ -0,0 +1,87 @@
|
|||
<?PHP
|
||||
|
||||
class Route{
|
||||
|
||||
public static $routes = Array();
|
||||
public static $routes404 = Array();
|
||||
public static $path;
|
||||
|
||||
public static function init(){
|
||||
|
||||
$parsed_url = parse_url($_SERVER['REQUEST_URI']);//URI zerlegen
|
||||
|
||||
if(isset($parsed_url['path'])){
|
||||
self::$path = trim($parsed_url['path'],'/');
|
||||
}else{
|
||||
self::$path = '';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function add($expression,$function){
|
||||
|
||||
array_push(self::$routes,Array(
|
||||
'expression'=>$expression,
|
||||
'function'=>$function
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
public static function add404($function){
|
||||
|
||||
array_push(self::$routes404,$function);
|
||||
|
||||
}
|
||||
|
||||
public static function run(){
|
||||
|
||||
$route_found = false;
|
||||
|
||||
foreach(self::$routes as $route){
|
||||
|
||||
if(Config::get('basepath')){
|
||||
|
||||
$route['expression'] = '('.Config::get('basepath').')/'.$route['expression'];
|
||||
|
||||
}
|
||||
|
||||
//Add 'find string start' automatically
|
||||
$route['expression'] = '^'.$route['expression'];
|
||||
|
||||
//Add 'find string end' automatically
|
||||
$route['expression'] = $route['expression'].'$';
|
||||
|
||||
//check match
|
||||
if(preg_match('#'.$route['expression'].'#',self::$path,$matches)){
|
||||
|
||||
//echo $expression;
|
||||
|
||||
array_shift($matches);//Always remove first element. This contains the whole string
|
||||
|
||||
if(Config::get('basepath')){
|
||||
|
||||
array_shift($matches);//Remove Basepath
|
||||
|
||||
}
|
||||
|
||||
call_user_func_array($route['function'], $matches);
|
||||
|
||||
$route_found = true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!$route_found){
|
||||
|
||||
foreach(self::$routes404 as $route404){
|
||||
|
||||
call_user_func_array($route404, Array(self::$path));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
39
index.php
Normal file
39
index.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?PHP
|
||||
|
||||
//include
|
||||
include('Config.php');
|
||||
include('Route.php');
|
||||
|
||||
//config
|
||||
Config::set('basepath','');
|
||||
|
||||
//init routing
|
||||
Route::init();
|
||||
|
||||
//base route
|
||||
Route::add('',function(){
|
||||
//Do something
|
||||
echo 'Welcome :-)';
|
||||
});
|
||||
|
||||
//simple route
|
||||
Route::add('test.html',function(){
|
||||
//Do something
|
||||
echo 'Hello from test.html';
|
||||
});
|
||||
|
||||
//complex route with parameter
|
||||
Route::add('user/(.*)/edit',function($id){
|
||||
//Do something
|
||||
echo 'Edit user with id '.$id;
|
||||
});
|
||||
|
||||
Route::add404(function($url){
|
||||
|
||||
//Send 404 Header
|
||||
header("HTTP/1.0 404 Not Found");
|
||||
echo '404 :-(';
|
||||
|
||||
});
|
||||
|
||||
Route::run();
|
Loading…
Reference in New Issue
Block a user