Download the PHP package kafkiansky/symfony-middleware without Composer
On this page you can find all versions of the php package kafkiansky/symfony-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kafkiansky/symfony-middleware
More information about kafkiansky/symfony-middleware
Files in kafkiansky/symfony-middleware
Package symfony-middleware
Short Description PSR-15 Middleware for symfony.
License MIT
Informations about the package symfony-middleware
PSR-15 middleware now in Symfony
Contents
- Installation
- Configuration
- Usage
- Examples
- Customization
- Caching
- Real World Example
- Middlewares
- Testing
- License
Installation
Configuration
Ensure you have added bundle in config/bundles.php
:
Create the configuration file in packages/symiddleware.yaml
:
Usage
Each middleware must implement the Psr\Http\Server\MiddlewareInterface
interface. Thanks for symfony autoconfiguration now the middleware registry knows your middleware.
So that middlewares can start execution, they must be defined on controller class and/or on controller method.
If controller is invokable, middleware can be defined just on controller class:
groups
If you want to use the list of middlewares, you can define middleware group inside symfony_middleware.yaml
configuration file:
Now define this middleware on controller class or method:
Pay attention to the if
parameter in configuration file. This parameter tells the middleware runner when the middleware group can be run.
If false, this middleware will not be executed.
global
If you want to run the list of middleware every request, you need the global
middleware section. This keyword is reserved and if
parameter is not supported.
Now the App\Controller\SetCorsHeaders
middleware will execute on every request.
Examples
-
Simple middleware that modifies request:
-
Middleware that modifies response:
- Middleware that stop execution:
In this example controller will not be executed.
- Stop execution with symfony response:
You can compose middleware group with single middleware, use list of Middleware
attributes and so on. All the following examples will work:
Also, you can use nested groups:
Duplicated middlewares will be removed.
Customization
PSR middlewares and Symfony has different incompatible Request objects. If your middleware going to change the request object,
only attributes
, query params
, headers
and parsed body
will be copied from psr request to symfony request.
If you wish to change this behaviour, you may change the Kafkiansky\SymfonyMiddleware\Psr\PsrRequestCloner
interface binding it to your realization.
Caching
Package use caching on production environment to prevent reflection usage. First of all, package will search of the app.cache_middleware
parameter. If package doesn't find it,
it's going to use the kernel.environment
definition and will cache attributes when it set to prod
.
Package will cache all controllers even if it doesn't found the attributes for it. This approach will allow to remember all the controllers and not use reflection further.
Real World Example
Imagine that you have some endpoints which requires authorization access via basic. Write middleware:
Middlewares
Handle HTTP Basic Auth PSR-15 middleware for Symfony
Testing
License
The MIT License (MIT). See License File for more information.
All versions of symfony-middleware with dependencies
symfony/dependency-injection Version ^7.0
symfony/event-dispatcher Version ^7.0
symfony/http-kernel Version ^7.0
symfony/config Version ^7.0
symfony/http-foundation Version ^7.0
psr/http-server-middleware Version ^1.0
psr/http-server-handler Version ^1.0
symfony/psr-http-message-bridge Version ^7.0
nyholm/psr7 Version ^1.4
symfony/cache Version ^7.0