Skip to content

Commit

Permalink
优化 Http 未找到路由处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Nov 8, 2019
1 parent c4a522f commit a144ccc
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/Server/Http/Middleware/RouteMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class RouteMiddleware implements MiddlewareInterface
*/
protected $route;

/**
* @ServerInject("HttpNotFoundHandler")
*
* @var \Imi\Server\Http\Error\IHttpNotFoundHandler
*/
protected $notFoundHandler;

/**
* 处理方法
* @param ServerRequestInterface $request
Expand All @@ -36,21 +43,21 @@ class RouteMiddleware implements MiddlewareInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// 路由解析
$result = $this->route->parse($request);
if(null === $result || !is_callable($result->callable))
{
// 未匹配到路由
$response = App::getBean('HttpNotFoundHandler')->handle($handler, $request, RequestContext::get('response'));
return $response;
}
else
{
RequestContext::set('routeResult', $result);
}
$response = $handler->handle($request);
RequestContext::set('response', $response);
return $response;
return RequestContext::use(function(&$context) use($request, $handler){
// 路由解析
$result = $this->route->parse($request);
if(null === $result || !is_callable($result->callable))
{
// 未匹配到路由
$response = $this->notFoundHandler->handle($handler, $request, $context['response']);
}
else
{
$context['routeResult'] = $result;
$response = $handler->handle($request);
}
return $context['response'] = $response;
});
}

}

0 comments on commit a144ccc

Please sign in to comment.