Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/teodoroleckie/router
⚡ Simple and fast router PSR-7, PSR-17, PSR-15
https://github.com/teodoroleckie/router
php php8 psr-15 psr-17 psr-7 routed router routes routing
Last synced: about 2 months ago
JSON representation
⚡ Simple and fast router PSR-7, PSR-17, PSR-15
- Host: GitHub
- URL: https://github.com/teodoroleckie/router
- Owner: teodoroleckie
- License: mit
- Created: 2021-04-20T12:41:04.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-05-13T08:48:46.000Z (over 3 years ago)
- Last Synced: 2024-05-02T04:13:12.886Z (8 months ago)
- Topics: php, php8, psr-15, psr-17, psr-7, routed, router, routes, routing
- Language: PHP
- Homepage:
- Size: 59.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
### Router:
[![Latest Version on Packagist](https://img.shields.io/packagist/v/tleckie/router.svg?style=flat-square)](https://packagist.org/packages/tleckie/router)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/teodoroleckie/router/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/teodoroleckie/router/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/teodoroleckie/router/badges/build.png?b=master)](https://scrutinizer-ci.com/g/teodoroleckie/router/build-status/master)
[![Total Downloads](https://img.shields.io/packagist/dt/tleckie/router.svg?style=flat-square)](https://packagist.org/packages/tleckie/router)Simple and fast router PSR-7, PSR-17, PSR-15
### Installation
You can install the package via composer:
```bash
composer require tleckie/router
```### Usage
```php
*/
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/(?[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*/
class ExampleMiddleware implements MiddlewareInterface
{
/**
* Objects that implement MiddlewareInterface will not receive routing parameters
* @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
all('/user/(?[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
post('/user/(?[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,