2020-01-08 09:25:45 -06:00
|
|
|
<?php
|
|
|
|
echo <<<EOD
|
|
|
|
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="/get-post-sample">get+post example</a></li>
|
|
|
|
<li><a href="/test.html">test.html</a></li>
|
|
|
|
<li><a href="/aTrailingSlashDoesNotMatters">aTrailingSlashDoesNotMatters</a></li>
|
|
|
|
<li><a href="/aTrailingSlashDoesNotMatters/">aTrailingSlashDoesNotMatters/</a></li>
|
|
|
|
<li><a href="/theCaseDoesNotMatters">theCaseDoesNotMatters</a></li>
|
|
|
|
<li><a href="/thecasedoesnotmatters">thecasedoesnotmatters</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>
|
|
|
|
EOD;
|
2019-01-31 03:17:40 -06:00
|
|
|
|
2018-03-13 10:01:52 -05:00
|
|
|
// Include router class
|
2020-01-08 09:25:45 -06:00
|
|
|
include 'Route.php';
|
2015-09-10 06:37:52 -05:00
|
|
|
|
2018-03-13 10:01:52 -05:00
|
|
|
// Add base route (startpage)
|
2020-01-08 09:25:45 -06: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
|
2020-01-08 09:25:45 -06:00
|
|
|
Route::add('/index.php', function() {
|
|
|
|
echo 'You are not really 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
|
2020-01-08 09:25:45 -06:00
|
|
|
// TODO: Fix this for some web servers
|
|
|
|
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
|
|
|
|
2019-02-21 17:10:47 -06:00
|
|
|
// Post route example
|
2020-01-08 09:25:45 -06:00
|
|
|
Route::add('/contact-form', function() {
|
|
|
|
echo '<form method="post"><input type="text" name="test"><input type="submit" value="send"></form>';
|
|
|
|
}, 'get');
|
2018-03-13 10:01:52 -05:00
|
|
|
|
|
|
|
// Post route example
|
2020-01-08 09:25:45 -06:00
|
|
|
Route::add('/contact-form', function() {
|
|
|
|
echo 'Hey! The form has been sent:<br>';
|
2018-03-13 10:01:52 -05:00
|
|
|
print_r($_POST);
|
2020-01-08 09:25:45 -06:00
|
|
|
}, 'post');
|
2018-03-13 10:01:52 -05:00
|
|
|
|
2019-02-21 17:06:38 -06:00
|
|
|
// Get and Post route example
|
2020-01-08 09:25:45 -06:00
|
|
|
Route::add('/get-post-sample', function() {
|
2019-02-21 17:06:38 -06:00
|
|
|
echo 'You can GET this page and also POST this form back to it';
|
2020-01-08 09:25:45 -06:00
|
|
|
echo '<form method="post"><input type="text" name="input"><input type="submit" value="send"></form>';
|
|
|
|
if (isset($_POST['input'])) {
|
|
|
|
echo 'I also received a POST with this data:<br>';
|
2019-02-21 17:06:38 -06:00
|
|
|
print_r($_POST);
|
|
|
|
}
|
2020-01-08 09:25:45 -06:00
|
|
|
}, ['get','post']);
|
2019-02-21 17:06:38 -06:00
|
|
|
|
2018-03-13 10:01:52 -05:00
|
|
|
// Route with regexp parameter
|
|
|
|
// Be aware that (.*) will match / (slash) too. For example: /user/foo/bar/edit
|
2020-01-08 09:25:45 -06:00
|
|
|
// Also users could inject SQL statements 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
|
2020-01-08 09:25:45 -06:00
|
|
|
Route::add('/user/(.*)/edit', function($id) {
|
|
|
|
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
|
2020-01-08 09:25:45 -06: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
|
2020-01-08 09:25:45 -06:00
|
|
|
Route::add('/(.*)/(.*)/(.*)/(.*)', function($var1,$var2,$var3,$var4) {
|
|
|
|
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
|
2020-01-08 09:25:45 -06:00
|
|
|
// TODO: Fix this; it'll get triggered
|
|
|
|
Route::add('/foo/bar/foo/bar', function() {
|
|
|
|
echo 'This is the second match <br>';
|
2015-09-10 07:09:42 -05:00
|
|
|
});
|
|
|
|
|
2019-01-31 03:54:07 -06:00
|
|
|
// Trailing slash example
|
2020-01-08 09:25:45 -06:00
|
|
|
Route::add('/aTrailingSlashDoesNotMatter', function() {
|
|
|
|
echo 'a trailing slash does not matter<br>';
|
2019-01-31 03:54:07 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// Case example
|
2020-01-08 09:25:45 -06:00
|
|
|
Route::add('/theCaseDoesNotMatter',function(){
|
|
|
|
echo 'the case does not matter<br>';
|
2019-01-31 03:54:07 -06:00
|
|
|
});
|
|
|
|
|
2018-03-13 10:01:52 -05:00
|
|
|
// 405 test
|
2020-01-08 09:25:45 -06:00
|
|
|
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';
|
2020-01-08 09:25:45 -06:00
|
|
|
}, 'patch');
|
2015-09-10 07:09:42 -05:00
|
|
|
|
2018-03-12 10:49:55 -05:00
|
|
|
// Add a 404 not found route
|
2020-01-08 09:25:45 -06:00
|
|
|
Route::pathNotFound(function($path) {
|
|
|
|
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
|
2020-01-08 09:25:45 -06:00
|
|
|
Route::methodNotAllowed(function($path, $method) {
|
|
|
|
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-12 11:12:05 -05:00
|
|
|
|
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-12 11:12:05 -05:00
|
|
|
|
2019-01-31 03:54:07 -06:00
|
|
|
// Enable case sensitive mode and trailing slashes by setting both to true
|
|
|
|
// Route::run('/', true, true);
|