PSR-15 middleware that makes it possible to add conditional middlewares, based on request
- PHP >= 7.1
- A PSR-7 http message implementation (Diactoros, Guzzle, Slim, etc...)
- A PSR-15 middleware dispatcher
This package is installable and autoloadable via Composer as zakirullin/conditional-middleware.
composer require zakirullin/conditional-middleware
$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'),
...
]);
__construct(
callable $shouldProtect,
callable $getIdentity,
string $secret,
string $attribute = self::ATTRIBUTE,
int $ttl = self::TTL,
string $algorithm = self::ALGORITHM
)
The session name. If it's not defined, the default PHPSESSID
will be used.
The MIT License (MIT). Please see LICENSE for more information.