https://github.com/zakirullin/conditional-middleware
PSR-15 middleware that makes it possible to add conditional middlewares, based on request
https://github.com/zakirullin/conditional-middleware
middleware middleware-pipeline psr-15 psr-7
Last synced: about 2 months ago
JSON representation
PSR-15 middleware that makes it possible to add conditional middlewares, based on request
- Host: GitHub
- URL: https://github.com/zakirullin/conditional-middleware
- Owner: zakirullin
- License: mit
- Created: 2018-05-14T11:40:36.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-25T09:25:38.000Z (about 6 years ago)
- Last Synced: 2025-01-13T10:08:38.220Z (3 months ago)
- Topics: middleware, middleware-pipeline, psr-15, psr-7
- Language: PHP
- Size: 7.81 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# zakirullin/conditional-middleware
[](https://travis-ci.org/zakirullin/conditional-middleware)
[](https://scrutinizer-ci.com/g/zakirullin/conditional-middleware/)


[](LICENSE)PSR-15 middleware that makes it possible to add conditional middlewares, based on `request`
## Requirements
* PHP >= 7.1
* A [PSR-7](https://packagist.org/providers/psr/http-message-implementation) http message implementation ([Diactoros](https://github.com/zendframework/zend-diactoros), [Guzzle](https://github.com/guzzle/psr7), [Slim](https://github.com/slimphp/Slim), etc...)
* A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)## Installation
This package is installable and autoloadable via Composer as [zakirullin/conditional-middleware](https://packagist.org/packages/zakirullin/conditional-middleware).
```sh
composer require zakirullin/conditional-middleware
```## PHP
```php
$shouldProtect = function (\Psr\Http\Message\ServerRequestInterface $request) {
$handler = $request->getAttribute('handler');
return $handler != 'login';
};
$getIdentity = function (\Psr\Http\Message\ServerRequestInterface $request) {
$session = $request->getAttribute('session');
return [$session->get('userId')];
};$dispatcher = new Dispatcher([
...
new \Zakirullin\Middlewares\CSRF($shouldProtect, $getIdentity, 'secret'),
...
]);
```## Options
```php
__construct(
callable $shouldProtect,
callable $getIdentity,
string $secret,
string $attribute = self::ATTRIBUTE,
int $ttl = self::TTL,
string $algorithm = self::ALGORITHM
)
```#### `name(string $name)`
The session name. If it's not defined, the default `PHPSESSID` will be used.
---
The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.