2020-01-08 09:25:45 -06:00
< ? php
2020-01-10 16:13:53 -06:00
2020-01-14 03:42:38 -06:00
// Use this namespace
2020-01-14 04:44:26 -06:00
use Steampixel\Route ;
2020-01-14 03:42:38 -06:00
// Include router class
2020-01-14 05:03:03 -06:00
include 'src/Steampixel/Route.php' ;
2020-01-14 03:42:38 -06:00
2020-05-15 05:02:26 -05:00
// Define a global basepath
define ( 'BASEPATH' , '/' );
2020-05-15 05:08:23 -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
// define('BASEPATH','/api/v1');
2020-01-10 15:56:48 -06:00
function navi () {
2020-05-15 05:02:26 -05:00
echo '
2020-01-10 15:56:48 -06:00
Navigation :
< ul >
2020-05-15 05:02:26 -05:00
< li >< a href = " '.BASEPATH.' " > home </ a ></ li >
< li >< a href = " '.BASEPATH.'index.php " > index . php </ a ></ li >
< li >< a href = " '.BASEPATH.'user/3/edit " > edit user 3 </ a ></ li >
< li >< a href = " '.BASEPATH.'foo/5/bar " > foo 5 bar </ a ></ li >
< li >< a href = " '.BASEPATH.'foo/bar/foo/bar " > long route example </ a ></ li >
< li >< a href = " '.BASEPATH.'contact-form " > contact form </ a ></ li >
< li >< a href = " '.BASEPATH.'get-post-sample " > get + post example </ a ></ li >
< li >< a href = " '.BASEPATH.'test.html " > test . html </ a ></ li >
2020-09-07 03:21:29 -05:00
< li >< a href = " '.BASEPATH.'blog/how-to-use-include-example " > How to push data to included files </ a ></ li >
2020-05-15 05:02:26 -05:00
< li >< a href = " '.BASEPATH.'phpinfo " > PHP Info </ a ></ li >
2021-01-14 08:07:02 -06:00
< li >< a href = " '.BASEPATH.'äöü " > Non english route : german </ a ></ li >
< li >< a href = " '.BASEPATH.'الرقص-العربي " > Non english route : arabic </ a ></ li >
2021-02-08 04:00:50 -06:00
< li >< a href = " '.BASEPATH.'global/test123 " > Inject variables to local scope </ a ></ li >
2021-02-08 03:52:14 -06:00
< li >< a href = " '.BASEPATH.'arrow/test123 " > Arrow function test ( please enable this route first ) </ a ></ li >
2020-05-15 05:02:26 -05:00
< li >< a href = " '.BASEPATH.'aTrailingSlashDoesNotMatter " > aTrailingSlashDoesNotMatter </ a ></ li >
< li >< a href = " '.BASEPATH.'aTrailingSlashDoesNotMatter/ " > aTrailingSlashDoesNotMatter /</ a ></ li >
< li >< a href = " '.BASEPATH.'theCaseDoesNotMatter " > theCaseDoesNotMatter </ a ></ li >
< li >< a href = " '.BASEPATH.'thecasedoesnotmatter " > thecasedoesnotmatter </ a ></ li >
< li >< a href = " '.BASEPATH.'this-route-is-not-defined " > 404 Test </ a ></ li >
< li >< a href = " '.BASEPATH.'this-route-is-defined " > 405 Test </ a ></ li >
2020-01-10 15:56:48 -06:00
</ ul >
2020-05-15 05:02:26 -05:00
' ;
2020-01-10 15:56:48 -06:00
}
2019-01-31 03:17:40 -06:00
2018-03-13 10:01:52 -05:00
// Add base route (startpage)
2020-01-08 09:25:45 -06:00
Route :: add ( '/' , function () {
2020-01-10 15:56:48 -06:00
navi ();
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 () {
2020-01-10 15:56:48 -06:00
navi ();
2020-01-08 09:25:45 -06:00
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-09-24 01:54:38 -05:00
Route :: add ( '/test.html' , function () {
2020-09-07 03:21:29 -05:00
navi ();
2020-09-24 01:54:38 -05:00
echo 'Hello from test.html' ;
2020-09-07 03:21:29 -05:00
});
// This example shows how to include files and how to push data to them
2020-09-24 01:54:38 -05:00
Route :: add ( '/blog/([a-z-0-9-]*)' , function ( $slug ) {
2020-01-10 15:56:48 -06:00
navi ();
2020-09-24 01:54:38 -05:00
include ( 'include-example.php' );
2015-09-10 06:37:52 -05:00
});
2017-10-25 01:25:13 -05:00
2020-01-10 16:13:53 -06:00
// This route is for debugging only
// It simply prints out some php infos
// Do not use this route on production systems!
Route :: add ( '/phpinfo' , function () {
navi ();
phpinfo ();
});
2020-07-23 04:38:11 -05:00
// Get route example
2020-01-08 09:25:45 -06:00
Route :: add ( '/contact-form' , function () {
2020-01-10 15:56:48 -06:00
navi ();
2020-01-08 09:25:45 -06:00
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 () {
2020-01-10 15:56:48 -06:00
navi ();
2020-01-08 09:25:45 -06:00
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 () {
2020-01-10 15:56:48 -06:00
navi ();
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 ) {
2020-01-10 15:56:48 -06:00
navi ();
2020-01-08 09:25:45 -06: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
2020-01-08 09:25:45 -06:00
Route :: add ( '/foo/([0-9]*)/bar' , function ( $var1 ) {
2020-01-10 15:56:48 -06:00
navi ();
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 ) {
2020-01-10 15:56:48 -06:00
navi ();
2020-01-08 09:25:45 -06: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
2020-01-11 15:44:56 -06:00
// By default this route gets never triggered because the route before matches too
2020-01-08 09:25:45 -06:00
Route :: add ( '/foo/bar/foo/bar' , function () {
2020-01-11 15:44:56 -06:00
echo 'This is the second match (This route should only work in multi match mode) <br>' ;
2015-09-10 07:09:42 -05:00
});
2021-01-14 08:07:02 -06:00
// Route with non english letters: german example
2021-01-14 07:54:43 -06:00
Route :: add ( '/äöü' , function () {
2021-01-14 08:07:02 -06:00
navi ();
echo 'German example. Non english letters should work too <br>' ;
});
// Route with non english letters: arabic example
Route :: add ( '/الرقص-العربي' , function () {
navi ();
echo 'Arabic example. Non english letters should work too <br>' ;
2021-01-14 07:54:43 -06:00
});
2021-02-08 04:00:50 -06:00
// Use variables from global scope
// You can use for example use() to inject variables to local scope
// You can use global to register the variable in local scope
$foo = 'foo' ;
$bar = 'bar' ;
Route :: add ( '/global/([a-z-0-9-]*)' , function ( $param ) use ( $foo ) {
global $bar ;
navi ();
echo 'The param is ' . $param . '<br/>' ;
echo 'Foo is ' . $foo . '<br/>' ;
echo 'Bar is ' . $bar . '<br/>' ;
});
2021-01-14 08:52:23 -06:00
// Return example
2021-02-08 04:00:50 -06:00
// Returned data gets printed
2021-01-14 08:52:23 -06:00
Route :: add ( '/return' , function () {
navi ();
return 'This text gets returned by the add method' ;
});
// Arrow function example
// Note: You can use this example only if you are on PHP 7.4 or higher
2021-02-08 03:52:14 -06:00
// $bar = 'bar';
// Route::add('/arrow/([a-z-0-9-]*)', fn($foo) => navi().'This is a working arrow function example. <br/> Parameter: '.$foo. ' <br/> Variable from global scope: '.$bar );
2021-01-14 08:52:23 -06:00
2019-01-31 03:54:07 -06:00
// Trailing slash example
2020-01-08 09:25:45 -06:00
Route :: add ( '/aTrailingSlashDoesNotMatter' , function () {
2020-01-10 15:56:48 -06:00
navi ();
2020-01-08 09:25:45 -06:00
echo 'a trailing slash does not matter<br>' ;
2019-01-31 03:54:07 -06:00
});
// Case example
2020-01-10 15:56:48 -06:00
Route :: add ( '/theCaseDoesNotMatter' , function () {
navi ();
2020-01-08 09:25:45 -06:00
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 () {
2020-01-10 15:56:48 -06:00
navi ();
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 ) {
2020-01-11 15:18:06 -06:00
// Do not forget to send a status header back to the client
// The router will not send any headers by default
// So you will have the full flexibility to handle this case
header ( 'HTTP/1.0 404 Not Found' );
2020-01-10 15:56:48 -06:00
navi ();
2020-01-08 09:25:45 -06: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
2020-01-08 09:25:45 -06:00
Route :: methodNotAllowed ( function ( $path , $method ) {
2020-01-11 15:18:06 -06:00
// Do not forget to send a status header back to the client
// The router will not send any headers by default
// So you will have the full flexibility to handle this case
header ( 'HTTP/1.0 405 Method Not Allowed' );
2020-01-10 15:56:48 -06:00
navi ();
2020-01-08 09:25:45 -06: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
2020-05-15 05:02:26 -05:00
Route :: run ( BASEPATH );
2018-03-12 11:12:05 -05:00
2020-01-11 15:44:56 -06:00
// Enable case sensitive mode, trailing slashes and multi match mode by setting the params to true
2020-05-15 05:08:23 -05:00
// Route::run(BASEPATH, true, true, true);