PHP code example of tuum / router

1. Go to this page and download the library: Download tuum/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/ */

    

tuum / router example snippets


use Tuum\Routing\Matcher;
$matched = Matcher::verify( $pattern, $path, $method);

$matched = Matcher::verify('get:/path/{id:i}', '/path/1001', 'get');
if ($matched) {
    $id = $matched['id'];
}

$matched = Matcher::verify('/path/to/*', '/path/to/more/route', 'ignored');
if ($matched) {
    echo $matched['matched']; // '/path/to/'
    echo $matched['trailing']; // 'more/route'
}

$matcher = new Matcher;
$matched = $matcher('/my/resource/*', '/my/resource/1001', 'get');

Matcher::verify( 'get:/path/{id}', '/path/1234', '*' );

$router = new Router([
    'get:/path/' => 'index',
    'get:/path/{id}' => 'get',
]);
$router->addRoute('put:/path/{id}', 'put');
$matched = $router->match('/path/123');
if($matched) {
    $method = $matched[0]; // the handler
    $params = $matched[1]; // matched parameter
    $id = $params['id'];
}

$router = new Router();
$routes = $router->getRouting();
$routes->any('/', 'top')
	->name('top');
$routes->get('/welcom', function(){ echo 'welcome';})
	->before('UserNameFilter');

$matched = $router->match('/welcom', 'get');
echo get_class($matched[0]); // Tuum\Routing\Handler

$routes->{method}($routePattern, $handler)
	->name($route_name)
	->before($filter_name)
	->after($may_not_work)
	->params($default_parameter);

echo $route->handle();
echo $route->name();
echo $route->before();
echo $route->after();
echo $route->params();
echo $route->path();
echo $route->method();
echo $route->trailing();
echo $route->matched();

$routes->group([
		'pattern' => '/admin/',
		'handler' => 'Admin\Controller\',
		'before' => 'AdminAuth',
	], 
	function($routes) {
		/** @var RouteCollector $routes */
		$routes->get('/', 'MainController');
	});
$matched = $route->match('/admin/', 'get');

$router = new Route();
$routes = $router->getRouting();
$routes->get('/sample/{id}', 'sample' )->name('sample');

$reverse = $router->getReverseRoute();
$route = $reverse->generate('sample',['id'=>'123']);