PHP code example of digphp / router
1. Go to this page and download the library: Download digphp/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/ */
digphp / router example snippets php
$router = new \DigPHP\Router\Router;
php
$router->get('/path1/{id:\d+}', 'somehandler1');
$router->get('/path2[/{id:\d+}]', 'somehandler2');
$router->addGroup('/group', function (Collector $collector) {
$collector->bindMiddlewares(['somemiddleware1', 'somemiddleware2']);
$collector->bindParams([
'q' => '111',
]);
$collector->get('/sub1', 'otherhandler1');
$collector->get('/sub2', 'otherhandler2', 'name1', ['middleware3']);
$collector->get('/sub3/{id:\d+}', 'otherhandler3', 'name2', ['middleware3']);
});
$res = $router->dispatch('GET', '/path2/33');
// Array
// (
// [0] => 1
// [1] => somehandler2
// [2] => Array
// (
// [id] => 33
// )
// [3] => Array
// (
// )
// [4] => Array
// (
// )
// )
$res = $router->dispatch('GET', '/group/sub1');
// Array
// (
// [0] => 1
// [1] => otherhandler1
// [2] => Array
// (
// )
// [3] => Array
// (
// [0] => somemiddleware1
// [1] => somemiddleware2
// )
// [4] => Array
// (
// [q] => 111
// )
// )
$res = $router->dispatch('GET', '/group/sub2');
// Array
// (
// [0] => 1
// [1] => otherhandler2
// [2] => Array
// (
// )
// [3] => Array
// (
// [0] => middleware3
// [1] => somemiddleware1
// [2] => somemiddleware2
// )
// [4] => Array
// (
// [q] => 111
// )
// )
$res = $router->dispatch('GET', '/group/sub3/11');
// Array
// (
// [0] => 1
// [1] => otherhandler3
// [2] => Array
// (
// [id] => 11
// )
// [3] => Array
// (
// [0] => middleware3
// [1] => somemiddleware1
// [2] => somemiddleware2
// )
// [4] => Array
// (
// [q] => 111
// )
// )
$res = $router->build('name2', ['id' => 11]);
// /group/sub3/11