tleckie / router
Simple and fast router PSR-7, PSR-17, PSR-15
Fund package maintenance!
teodoroleckie
www.paypal.com/donate?business=ZHYA2MTGA4884¤cy_code=USD
Requires
- php: ^8
- httpsoft/http-emitter: ^1.0
- httpsoft/http-message: ^1.0
- psr/http-factory: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: v3.0.0-beta.2
- infection/infection: ^0.21.5
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-12-13 16:20:14 UTC
README
Router:
Simple and fast router PSR-7, PSR-17, PSR-15
Installation
You can install the package via composer:
composer require tleckie/router
Usage
<?php use HttpSoft\Emitter\SapiEmitter; use HttpSoft\Message\ResponseFactory; use HttpSoft\Message\ServerRequestFactory; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Tleckie\Router\Router; $router = new Router( new ServerRequestFactory(), new ResponseFactory(), new SapiEmitter() ); /** * Class UserController * * @author Teodoro Leckie Westberg <teodoroleckie@gmail.com> */ class UserController { /** * @param ServerRequestInterface $request * @param ResponseInterface $response * @param array $params * @return ResponseInterface */ public function retrieveUserAction(ServerRequestInterface $request, ResponseInterface $response, array $params): ResponseInterface { $response->getBody()->write(sprintf(" CONTROLLER USER ID: %s", $params['id'])); return $response->withHeader('Content-Type', 'text/html'); } } // routing with middleware $router->get('/user/(?<id>[0-9]+)/', static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) { $response = $handler->handle($request); $response->getBody()->write(sprintf(' (ROUTES MIDDLEWARE ID: #%s) ', $params['id'])); return $response; }, [new UserController, 'retrieveUserAction'] );
Add global middleware
The middleware added in the add method is always executed.
<?php require_once "vendor/autoload.php"; use HttpSoft\Emitter\SapiEmitter; use HttpSoft\Message\ResponseFactory; use HttpSoft\Message\ServerRequestFactory; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Tleckie\Router\Router; $router = new Router( new ServerRequestFactory(), new ResponseFactory(), new SapiEmitter() ); /** * Class ExampleMiddleware * @author Teodoro Leckie Westberg <teodoroleckie@gmail.com> */ class ExampleMiddleware implements MiddlewareInterface { /** * <strong>Objects that implement MiddlewareInterface will not receive routing parameters</strong> * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $response = $handler->handle($request); $response->getBody()->write(' (GLOBAL 1)'); return $response; } } // Add app closure middleware. The closure middleware will receive the routing parameters $router->add(static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params){ $response = $handler->handle($request); $response->getBody()->write(sprintf(' (GLOBAL ID: %s)', $params['id'])); return $response; }); // app middleware. $router->add(new ExampleMiddleware());
Support for all methods.
<?php require_once "vendor/autoload.php"; use HttpSoft\Emitter\SapiEmitter; use HttpSoft\Message\ResponseFactory; use HttpSoft\Message\ServerRequestFactory; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Tleckie\Router\Router; $router = new Router( new ServerRequestFactory(), new ResponseFactory(), new SapiEmitter() ); $router->all('/user/(?<id>[0-9]+)/', static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) { $response = $handler->handle($request); $response->getBody()->write(' (ROUTES MIDDLEWARE #1#)' . $params['id']); return $response; }, static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) { $response = $handler->handle($request); $response->getBody()->write(' (ROUTES MIDDLEWARE #2#)' . $params['id']); return $response; }, [new UserController, 'retrieveUserAction'] );
<?php require_once "vendor/autoload.php"; use HttpSoft\Emitter\SapiEmitter; use HttpSoft\Message\ResponseFactory; use HttpSoft\Message\ServerRequestFactory; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Tleckie\Router\Exception\RouteNotFoundException; use Tleckie\Router\Router; $router = new Router( new ServerRequestFactory(), new ResponseFactory(), new SapiEmitter() ); $router->post('/user/(?<id>[0-9]+)/', static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) { $response = $handler->handle($request); $response->getBody()->write(' (ROUTES MIDDLEWARE #1#)' . $params['id']); return $response; }, static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) { $response = $handler->handle($request); $response->getBody()->write(' (ROUTES MIDDLEWARE #2#)' . $params['id']); return $response; }, [new UserController, 'retrieveUserAction'] ); try { $router->run( $_SERVER['REQUEST_METHOD'], $_SERVER['REDIRECT_URL'] ?? '/' ); } catch (RouteNotFoundException $exception) { // handle 404 }
Methods:
GET, POST, HEAD, PATCH, OPTIONS, DELETE, PUT,