SimpleRouter/index.php

73 lines
1.7 KiB
PHP
Raw Normal View History

2015-09-10 06:37:52 -05:00
<?PHP
//include
include('Config.php');
include('Route.php');
2017-10-25 01:25:13 -05:00
//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');
2017-10-25 01:25:13 -05:00
2015-09-10 06:37:52 -05:00
//init routing
Route::init();
2015-09-10 07:09:42 -05:00
//base route (startpage)
2017-10-25 01:25:13 -05:00
Route::add('/',function(){
2015-09-10 06:37:52 -05:00
//Do something
echo 'Welcome :-)';
});
2015-09-10 07:09:42 -05:00
//base route
2017-10-25 01:25:13 -05:00
Route::add('/index.php',function(){
2015-09-10 07:09:42 -05:00
//Do something
echo 'You are not realy on index.php ;-)';
});
2015-09-10 06:37:52 -05:00
//simple route
2017-10-25 01:25:13 -05:00
Route::add('/test.html',function(){
2015-09-10 06:37:52 -05:00
//Do something
echo 'Hello from test.html';
});
2017-10-25 01:25:13 -05:00
//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
2017-10-25 01:25:13 -05:00
Route::add('/user/(.*)/edit',function($id){
2015-09-10 06:37:52 -05:00
//Do something
2015-09-10 07:09:42 -05:00
echo 'Edit user with id '.$id.'<br/>';
2015-09-10 06:37:52 -05:00
});
2015-09-10 07:09:42 -05:00
//accept only numbers as the second parameter. Other chars will result in a 404
2017-10-25 01:25:13 -05:00
Route::add('/foo/([0-9]*)/bar',function($var1){
2015-09-10 07:09:42 -05:00
//Do something
echo $var1.' is a great number!';
});
//long route
2017-10-25 01:25:13 -05:00
Route::add('/foo/bar/foo/bar',function(){
2015-09-10 07:09:42 -05:00
//Do something
echo 'hehe :-)<br/>';
});
//crazy route with parameters (Will be triggered on the route pattern above too because it matches too)
2017-10-25 01:25:13 -05:00
Route::add('/(.*)/(.*)/(.*)/(.*)',function($var1,$var2,$var3,$var4){
2015-09-10 07:09:42 -05:00
//Do something
echo 'You have entered: '.$var1.' / '.$var2.' / '.$var3.' / '.$var4.'<br/>';
});
//Add a 404 Not found Route
2015-09-10 06:37:52 -05:00
Route::add404(function($url){
2017-10-25 01:25:13 -05:00
2015-09-10 06:37:52 -05:00
//Send 404 Header
header("HTTP/1.0 404 Not Found");
2015-09-10 07:09:42 -05:00
echo '404 :-(<br/>';
echo $url.' not found!';
2017-10-25 01:25:13 -05:00
2015-09-10 06:37:52 -05:00
});
2017-10-25 01:25:13 -05:00
Route::run();