Skip to content

getAttribute方法

上面将匹配的路由信息放到attribute属性中,现在通过getAttribute方法获取。

php
$dispatched = $psr7Request->getAttribute(Dispatched::class);
// 获取中间件
$middlewares = $this->middlewares;
$registeredMiddlewares = [];
// 如果匹配到路由,则获取路由定义的中间件
if ($dispatched->isFound()) {
    $registeredMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod());
    $middlewares = array_merge($middlewares, $registeredMiddlewares);
}
// 对路由进行排序
if ($this->option?->isMustSortMiddlewares() || $registeredMiddlewares) {
    $middlewares = MiddlewareManager::sortMiddlewares($middlewares);
}