Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/delolmo/symfony-router
PSR-15 middleware to use the symfony/routing component and store the route attributes in the request.
https://github.com/delolmo/symfony-router
http middleware psr-15 psr-7 router symfony symfony-router
Last synced: 3 months ago
JSON representation
PSR-15 middleware to use the symfony/routing component and store the route attributes in the request.
- Host: GitHub
- URL: https://github.com/delolmo/symfony-router
- Owner: delolmo
- License: mit
- Created: 2018-09-30T08:18:13.000Z (over 6 years ago)
- Default Branch: v2.1.x
- Last Pushed: 2023-12-15T16:06:59.000Z (about 1 year ago)
- Last Synced: 2024-11-09T20:58:24.303Z (3 months ago)
- Topics: http, middleware, psr-15, psr-7, router, symfony, symfony-router
- Language: PHP
- Homepage:
- Size: 244 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-psr15-middlewares - delolmo/symfony-router - Use [symfony/routing](https://github.com/symfony/routing) (Packages / Router)
README
# delolmo/symfony-router
[![Packagist Version](https://img.shields.io/packagist/v/delolmo/symfony-router.svg?style=flat-square)](https://packagist.org/packages/delolmo/symfony-router)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Build Status](https://travis-ci.org/delolmo/symfony-router.svg)](https://travis-ci.org/delolmo/symfony-router)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/delolmo/symfony-router/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/delolmo/symfony-router/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/delolmo/symfony-router/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/delolmo/symfony-router/?branch=master)PSR-15 middleware to use the symfony/routing component and store the route attributes in the request.
## Requirements
* PHP ^8.1
* A [PSR-7 http library](https://github.com/middlewares/awesome-psr15-middlewares#psr-7-implementations)
* A [PSR-17 http factory](https://www.php-fig.org/psr/psr-17/)## Installation
This package is installable and autoloadable via Composer as [delolmo/symfony-router](https://packagist.org/packages/delolmo/symfony-router).
```sh
composer require delolmo/symfony-router
```You may also want to install [php-http/discovery](https://github.com/php-http/discovery) to autodetect well-known PSR-17 HTTP factory implementations.
## Example
Consider Symfony's PhpFileLoader to load route definitions from the following file:
``` php
# routes.phpuse App\Controller\BlogController;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;$routes = new RouteCollection();
$routes->add('blog_list', new Route('/blog', array(
'request-handler' => [BlogController::class, 'list']
)));
$routes->add('blog_show', new Route('/blog/{slug}', array(
'request-handler' => [BlogController::class, 'show']
)));return $routes;
```
For this example, we will be using `middlewares/utils` for a PSR-15 compliant
dispatcher. See [link](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)
for more PSR-15 implementations.This example uses a basic anonymous function to print the route's attributes:
```php
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\ServerRequest;
use Middlewares\Utils\Dispatcher;
use Middlewares\Utils\Factory;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\PhpFileLoader;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Router;$fileLocator = new FileLocator(array(__DIR__));
$router = new Router(
new PhpFileLoader($fileLocator),
'routes.php',
array('cache_dir' => __DIR__ . '/cache'),
new RequestContext('/')
);$factory = Factory::getRequestFactory();
$dispatcher = new Dispatcher([
new DelOlmo\Middleware\SymfonyRouterMiddleware($router, $factory),
function($request, $next) {
return new HtmlResponse(json_encode($request->getAttributes()));
}
]);// Try matching a /blog request
$response = $dispatcher->dispatch(new ServerRequest([], [], '/blog'));// Will return {"_route": "blog_list", "request-handler" => ["App\Controller\BlogController", "list"]}
$c->get('emitter')->emit($response);// Try matching a /blog/hello-world request
$response = $dispatcher->dispatch(new ServerRequest([], [], '/blog/hello-world'));// Will return {"_route": "blog_show", "request-handler" => ["App\Controller\BlogController", "show"], "slug" => "hello-world"}
$c->get('emitter')->emit($response);```
## Options
The constructor takes two arguments:
```php
__construct(
\Symfony\Component\Routing\Router $router,
\Psr\Http\Message\ResponseFactoryInterface $responseFactory
)
```The router instance to use and a PSR-17 factory to create the error
responses (`404` or `405`).