1. Go to this page and download the library: Download maer/router library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
maer / router example snippets
// Load composers autoloader
;
// Define routes
$r->get('/', function() {
return "Hello there";
});
// It also works with:
$r->post('/', function() {});
$r->put('/', function() {});
$r->delete('/', function() {});
$r->patch('/', function() {});
$r->options('/', function() {});
$r->head('/', function() {});
$r->connect('/', function() {});
$r->trace('/', function() {});
$r->any('/', function() {}); // Catches all methods
// ...or if you want to use some non-standard HTTP verb
$r->add('SOMEVERB', '/', function() {});
// ...or if you want to define multiple verbs at once
$r->add(['GET', 'POST', ...], function() {});
// Dispatch the router
$response = $r->dispatch();
echo $response;
// Match any alpha [a-z] character
$r->get('/something/(:alpha)', function($param) {
// Do stuff
});
// Match any numeric [0-9.,] character. It can also start with a -
$r->get('/something/(:num)', function($param) {
// Do stuff
});
// Match any alphanumeric [a-z0-9] character
$r->get('/something/(:alphanum)', function($param) {
// Do stuff
});
// Match any character (except /) [^/] character
$r->get('/something/(:any)', function($param) {
// Do stuff
});
// Catch-all. Match all routes, including / (.*)
$r->get('/something/(:any)', function($param) {
// Do stuff
});
// Append ? to making a parameter optional.
$r->get('/something/(:alpha)?', function($param = null){
// Matches /something and /something/anything
});
// Combine mutliple placeholders
$r->get('/something/(:alpha)/(:any)/(:alphanum)?', function($param, $param2, $param3 = null) {
// Do stuff
});
// Anonymous function
$r->get('/', function() {
// Something
});
// Class method
$r->get('/', ['Namespace\ClassName', 'methodName']);
// or
$r->get('/', 'Namespace\ClassName@methodName');
// Static class method
$r->get('/', 'Namespace\ClassName::methodName');
// Defining filters
$r->filter('myfilter', function() {
// Do some magic stuff.
});
$r->filter('anotherfilter', function() {
// Do some magic stuff.
});
// Add filter to your routes
$r->get('/something/', function() {
}, ['before' => 'myfilter', 'after' => 'anotherfilter']);
// Add multiple filters by combining them with |
$r->get('/something/', function() {
}, ['before' => 'myfilter|anotherfilter']);
// Name a route
$r->get('/something', function() {
}, ['name' => 'some-page']);
// Resolve a named route
echo $r->getRoute('some-page');
// Returns: /something
// With route parameters
$r->get('/something/(:any)/(:any)', function($param1, $param2) {
});
// Resolve and pass values for the placeholders
echo $r->getRoute('some-page', ['first', 'second']);
// Returns: /something/first/second
// A simple redirect
$r->redirect('/from/path', '/to/path');
// It works with absolute targets as well
$r->redirect('/foo', 'https://example.com');
// You can also register a named route as target
$r->redirect('/foo', null, [
'route' => ['someRouteName'],
// Add route arguments, if needed
'params' => ['argument1', 'argument2', ...]
]);
// If you want to redirect using another http code than 307 (default)
$r->redirect('/foo', '/bar', [
'code' => 301
]);
// Make a redirect to a named route
$r->toRoute('someRouteName', [
'argument1',
'argument2',
...
]);
// With another http code than 307 (default)
$r->toRoute('someRouteName', [], 301);
$r->notFound(function() {
return "Ops! The page was not found!";
});
// Callbacks can be in all the same formats as for routes
$r->methodNotAllowed(function() {
return "Ops! Method not allowed!";
});
// Callbacks can be in all the same formats as for routes
$r->resolver(function($callback) use($container) {
// The argument will always be an array with ['Class', 'method']
return [
$container->get($callback[0]),
$container[1]
];
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.