mamadou-aly-sy / router
A simple php router
2.0.0
2021-10-12 01:47 UTC
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-05-18 16:28:00 UTC
README
A simple php router
Requirements
- PHP version:
>=8.0
Initialization
<?php require_once './vendor/autoload.php'; $router = new \MamadouAlySy\Router( new \MamadouAlySy\RouteCollection() );
Routes Registration
$router->get('/', function () {/**/}); $router->post('/', function () {/**/}); $router->put('/', function () {/**/}); $router->delete('/', function () {/**/}); $router->any('/', function () {/**/});
Matching Routes
$route = $router->match('GET', '/'); // => returns a route if match or null if not match $route->getName(); // => returns the route name $route->getAction(); // => returns the route action $route->getParameters(); // => returns the route matched parameters
Dynamic Routes
$router->get('/user/:id', function () {/**/})->with('id', '[0-9]+'); $router->get('/:action/:name', function () {/**/});
Generating route url
$router->get('/edit/:id', function () {/**/}, 'app.edit'); $router->generateUri('app.edit', [id => 2]); // => returns /edit/2