中间件
<?php
use Hyperf\Nano\Factory\AppFactory;
require_once __DIR__ . '/vendor/autoload.php';
$app = AppFactory::create();
$app->get('/', function () {
return $this->request->getAttribute('key');
});
$app->addMiddleware(function ($request, $handler) {
$request = $request->withAttribute('key', 'value');
return $handler->handle($request);
});
$app->run();
除了闭包之外,所有 $app->addXXX() 方法还接受类名作为参数。可以传入对应的 Hyperf 类。