SimpleRouter/index.php

70 lines
1.8 KiB
PHP
Raw Normal View History

2015-09-10 06:37:52 -05:00
<?PHP
2018-03-12 10:49:55 -05:00
// Include needed files
2015-09-10 06:37:52 -05:00
include('Route.php');
2017-10-25 01:25:13 -05:00
2018-03-12 10:58:57 -05:00
// Init routing and configure basepath
2018-03-12 10:49:55 -05:00
// If your script lives in the web root folder use a / , leave it empty or do not define this config
2018-03-12 10:58:57 -05:00
Route::init('/');
2017-10-25 01:25:13 -05:00
2018-03-12 10:49:55 -05:00
// If your script lives in a subfolder you can use the following example
2018-03-12 10:58:57 -05:00
// Route::init('/api/v1');
2015-09-10 06:37:52 -05:00
2018-03-12 10:49:55 -05:00
// Base route (startpage)
2017-10-25 01:25:13 -05:00
Route::add('/',function(){
2018-03-12 10:49:55 -05:00
// Do something
2015-09-10 06:37:52 -05:00
echo 'Welcome :-)';
});
2018-03-12 10:49:55 -05:00
// Another base route example
2017-10-25 01:25:13 -05:00
Route::add('/index.php',function(){
2018-03-12 10:49:55 -05:00
// Do something
2015-09-10 07:09:42 -05:00
echo 'You are not realy on index.php ;-)';
});
2018-03-12 10:49:55 -05:00
// Simple route
2017-10-25 01:25:13 -05:00
Route::add('/test.html',function(){
2018-03-12 10:49:55 -05:00
// Do something
2015-09-10 06:37:52 -05:00
echo 'Hello from test.html';
});
2017-10-25 01:25:13 -05:00
2018-03-12 10:49:55 -05:00
// 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
2017-10-25 01:25:13 -05:00
Route::add('/user/(.*)/edit',function($id){
2018-03-12 10:49:55 -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
2018-03-12 10:49:55 -05:00
// Accept only numbers as the second parameter. Other characters will result in a 404
2017-10-25 01:25:13 -05:00
Route::add('/foo/([0-9]*)/bar',function($var1){
2018-03-12 10:49:55 -05:00
// Do something
2015-09-10 07:09:42 -05:00
echo $var1.' is a great number!';
});
2018-03-12 10:49:55 -05:00
// Long route example
2017-10-25 01:25:13 -05:00
Route::add('/foo/bar/foo/bar',function(){
2018-03-12 10:49:55 -05:00
// Do something
2015-09-10 07:09:42 -05:00
echo 'hehe :-)<br/>';
});
2018-03-12 10:49:55 -05:00
// 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){
2018-03-12 10:49:55 -05:00
// Do something
2015-09-10 07:09:42 -05:00
echo 'You have entered: '.$var1.' / '.$var2.' / '.$var3.' / '.$var4.'<br/>';
});
2018-03-12 10:49:55 -05:00
// 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
2018-03-12 10:49:55 -05:00
// Send 404 Header
2015-09-10 06:37:52 -05:00
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
2018-03-12 10:49:55 -05:00
// Check if any of the defined routes will match and execute them
2017-10-25 01:25:13 -05:00
Route::run();