/router

A simple php router

Primary LanguagePHPMIT LicenseMIT

Router

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