中间件

  1. <?php
  2. use Hyperf\Nano\Factory\AppFactory;
  3. require_once __DIR__ . '/vendor/autoload.php';
  4. $app = AppFactory::create();
  5. $app->get('/', function () {
  6. return $this->request->getAttribute('key');
  7. });
  8. $app->addMiddleware(function ($request, $handler) {
  9. $request = $request->withAttribute('key', 'value');
  10. return $handler->handle($request);
  11. });
  12. $app->run();

除了闭包之外,所有 $app->addXXX() 方法还接受类名作为参数。可以传入对应的 Hyperf 类。