php 通过 symfony/http-foundation(http请求响应封装) 框架捕获http请求
laravel kernel 类 为laravel http 入口
middleware 类似于 过滤器 可以执行请求处理 然后next 执行处理链 必定有 handle 方法 当需要处理请求前操作 在调用 $next 前修改请求信息 当需要处理请求后 或者 在响应上操作 则 在$next后 修改信息 且返回结果必定为 $response对象
public function handle($request, Closure $next)
{
// Check if we're dealing with CORS and if we should handle it
if (! $this->shouldRun($request)) {
return $next($request);
}
// For Preflight, return the Preflight response
if ($this->cors->isPreflightRequest($request)) {
$response = $this->cors->handlePreflightRequest($request);
$this->cors->varyHeader($response, 'Access-Control-Request-Method');
return $response;
}
// Add the headers on the Request Handled event as fallback in case of exceptions
if (class_exists(RequestHandled::class) && $this->container->bound('events')) {
$this->container->make('events')->listen(RequestHandled::class, function (RequestHandled $event) {
$this->addHeaders($event->request, $event->response);
});
}
// Handle the request
$response = $next($request);
if ($request->getMethod() === 'OPTIONS') {
$this->cors->varyHeader($response, 'Access-Control-Request-Method');
}
return $this->addHeaders($request, $response);
}