PHP code example of zakirullin / conditional-middleware

1. Go to this page and download the library: Download zakirullin/conditional-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zakirullin / conditional-middleware example snippets


$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
)