codin/router

Just another router

dev-master 2021-10-25 16:01 UTC

This package is auto-updated.

Last update: 2024-04-25 21:23:17 UTC


README

Simple routing component

$routes = new Router(); // RouterFactory::createFromPath('path/to/definitions');

$route = $routes->create('get', '/path/to/resource', 'controller@method');
$routes->append($route);

// or shortcut
$routes->get('/path/to/resource', 'controller@method');

// using grouping
$routes->group([
    'prefix' => '/api',
    'namespace' => 'API\\',
], static function ($routes) {
    $routes->get('/path/to/resource', 'controller@method');
})

$psr7request = new Request('post', 'foo/bar');
$matcher = new Matcher($routes);
try {
    $route = $matcher->match($psr7request);
} catch (Exceptions\RouteNotFound $e) {
    // do something with 404
}
$route->getController(); // returns controller@method