datapp/router

simple PSR-7 regular expression router

1.1.0 2021-04-01 07:23 UTC

This package is not auto-updated.

Last update: 2024-05-09 22:54:44 UTC


README

this router library uses regular expression for routing and is compatible to the PHP Standards Recommendations 7 (HTTP Message Interface), 15 (HTTP Handlers) and 17 (HTTP Factories).

Installation

composer require datapp/router

Usage

<?php

require 'vendor/autoload.php';

$router = new Router(new Dispatcher());
// route without middlewares
$router->addRoute(Route::create('GET', '/^(help)$/', new Help($responseFactory, $streamFactory)));
// route with middlewares
$router->addRoute(Route::create('POST', '/^(login)$/', new Login($responseFactory, $streamFactory))
                ->withMiddleware(new SessionMiddleware(), new AuthMiddleware()));
// using variables (named groups) in regex
$router->addRoute(Route::create('GET', '/^(user)\/(?<id>[\d]+)$/', new UserShow($responseFactory, $streamFactory))
                ->withMiddleware(new SessionMiddleware(), new AuthMiddleware()));