SimpleRouter/index.php

95 lines
2.9 KiB
PHP
Raw Normal View History

2015-09-10 06:37:52 -05:00
2018-03-13 10:01:52 -05:00
Navigation:
<ul>
<li><a href="/">home</a></li>
<li><a href="/index.php">index.php</a></li>
<li><a href="/user/3/edit">edit user 3</a></li>
<li><a href="/foo/5/bar">foo 5 bar</a></li>
<li><a href="/foo/bar/foo/bar">long route example</a></li>
<li><a href="/contact-form">contact form</a></li>
<li><a href="/test.html">test.html</a></li>
<li><a href="/this-route-is-not-defined">404 Test</a></li>
<li><a href="/this-route-is-defined">405 Test</a></li>
</ul>
2017-10-25 01:25:13 -05:00
2018-03-13 10:01:52 -05:00
<?PHP
2017-10-25 01:25:13 -05:00
2018-03-13 10:01:52 -05:00
// Include router class
include('Route.php');
2015-09-10 06:37:52 -05:00
2018-03-13 10:01:52 -05:00
// Add base route (startpage)
2017-10-25 01:25:13 -05:00
Route::add('/',function(){
2018-03-20 10:04:48 -05:00
echo 'Welcome :-)';
2015-09-10 06:37:52 -05:00
});
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-20 10:04:48 -05:00
echo 'You are not realy on index.php ;-)';
2015-09-10 07:09:42 -05:00
});
2018-03-13 10:01:52 -05:00
// Simple test route that simulates static html file
2017-10-25 01:25:13 -05:00
Route::add('/test.html',function(){
2018-03-20 10:04:48 -05:00
echo 'Hello from test.html';
2015-09-10 06:37:52 -05:00
});
2017-10-25 01:25:13 -05:00
2018-03-13 10:01:52 -05:00
// Post route example
Route::add('/contact-form',function(){
2018-03-20 10:04:48 -05:00
echo '<form method="post"><input type="text" name="test" /><input type="submit" value="send" /></form>';
2018-03-13 10:01:52 -05:00
},'get');
// Post route example
Route::add('/contact-form',function(){
2018-03-20 10:04:48 -05:00
echo 'Hey! The form has been sent:<br/>';
2018-03-13 10:01:52 -05:00
print_r($_POST);
},'post');
// Route with regexp parameter
// Be aware that (.*) will match / (slash) too. For example: /user/foo/bar/edit
// Also users could inject mysql-code or other untrusted data if you use (.*)
2018-03-12 10:49:55 -05:00
// 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-20 10:04:48 -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-13 10:01:52 -05:00
// Accept only numbers as parameter. Other characters will result in a 404 error
2017-10-25 01:25:13 -05:00
Route::add('/foo/([0-9]*)/bar',function($var1){
2018-03-20 10:04:48 -05:00
echo $var1.' is a great number!';
2015-09-10 07:09:42 -05:00
});
2018-03-13 10:01:52 -05:00
// Crazy route with parameters
Route::add('/(.*)/(.*)/(.*)/(.*)',function($var1,$var2,$var3,$var4){
2018-03-20 10:04:48 -05:00
echo 'This is the first match: '.$var1.' / '.$var2.' / '.$var3.' / '.$var4.'<br/>';
2018-03-13 10:01:52 -05:00
});
2018-03-12 10:49:55 -05:00
// Long route example
2018-03-13 10:01:52 -05:00
// This route gets never triggered because the route before matches too
2017-10-25 01:25:13 -05:00
Route::add('/foo/bar/foo/bar',function(){
2018-03-20 10:04:48 -05:00
echo 'This is the second match <br/>';
2015-09-10 07:09:42 -05:00
});
2018-03-13 10:01:52 -05:00
// 405 test
Route::add('/this-route-is-defined',function(){
2018-03-20 10:04:48 -05:00
echo 'You need to patch this route to see this content';
2018-03-13 10:01:52 -05:00
},'patch');
2015-09-10 07:09:42 -05:00
2018-03-12 10:49:55 -05:00
// Add a 404 not found route
2018-03-13 10:01:52 -05:00
Route::pathNotFound(function($path){
2018-03-20 10:04:48 -05:00
echo 'Error 404 :-(<br/>';
2018-03-13 10:01:52 -05:00
echo 'The requested path "'.$path.'" was not found!';
});
2017-10-25 01:25:13 -05:00
2018-03-13 10:01:52 -05:00
// Add a 405 method not allowed route
Route::methodNotAllowed(function($path, $method){
2018-03-20 10:04:48 -05:00
echo 'Error 405 :-(<br/>';
2018-03-13 10:01:52 -05:00
echo 'The requested path "'.$path.'" exists. But the request method "'.$method.'" is not allowed on this path!';
2015-09-10 06:37:52 -05:00
});
2017-10-25 01:25:13 -05:00
2018-03-13 10:01:52 -05:00
// Run the Router with the given Basepath
// If your script lives in the web root folder use a / or leave it empty
Route::run('/');
2018-03-13 10:01:52 -05:00
// If your script lives in a subfolder you can use the following example
// Do not forget to edit the basepath in .htaccess if you are on apache
// Route::run('/api/v1');
2018-03-13 10:01:52 -05:00
?>