PHP code example of haikara / middleware-stack

1. Go to this page and download the library: Download haikara/middleware-stack 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/ */

    

haikara / middleware-stack example snippets


// $containerはPSR-11:ContainerInterfaceの実装
// 後述のMiddleware1, Middleware2, Middleware3を登録済みのものとする
$handler = RequestHandler::createFromContainer($container);

// 中心になるAction
$action = static function () {
    echo 'Action' . PHP_EOL;
    return new Response();
};

$handler->addMiddleware($action);

// 一括登録
$handler->addMiddlewares([
    Middleware1::class,
    Middleware2::class,
    Middleware3::class,
]);

// 実行
$response = $handler->handle($request);

/*
 * Middleware3, Middleware2, Middleware1, $actionの順で実行される
 */

$handler = RequestHandler::createFromContainer($container);

$handler = new RequestHandler(fn (string $entry) => $entry);