use Illuminate\Routing\Router;
public function __construct(Application $app, Router $router)
{
$this->app = $app;
//路由类实例,由容器自动加载依赖而来
$this->router = $router;
//系统中间件
$router->middlewarePriority = $this->middlewarePriority;
//中间件分组
foreach ($this->middlewareGroups as $key => $middleware) {
$router->middlewareGroup($key, $middleware);
}
//注册中间件别名
foreach ($this->routeMiddleware as $key => $middleware) {
$router->aliasMiddleware($key, $middleware);
}
}
可以看到,laravel在实例化出kernel对象的同时,通过kernel的构造函数加载了系统中间件,依赖了application与route两个对象。并将自身的$middlewareGroups、routeMiddleware数组解析进了route对象里,在路由进行调用的时候就会把路由方法上绑定的中间件名在这里解析出实例来调用了,其中routeMiddleware为别名所用。,随后在index.php文件中马上就利用kernel的handle方法,传入了一个request对象,来处理这次的网页url请求。
public function handle($request)
{
try {
//启用http方法覆盖参数
$request->enableHttpMethodParameterOverride();
//通过路由发送请求
$response = $this->sendRequestThroughRouter($request);
} catch (Exception $e) {
$this->reportException($e);
$response = $this->renderException($request, $e);
} catch (Throwable $e) {
$this->reportException($e = new FatalThrowableError($e));
$response = $this->renderException($request, $e);
}
$this->app['events']->dispatch(
new Events\RequestHandled($request, $response)
);
return $response;
}
protected function sendRequestThroughRouter($request)
{
//将请求存入容器
$this->app->instance('request', $request);
//清除facade门面
Facade::clearResolvedInstance('request');
//初始化引导
$this->bootstrap();
//让请求进入中间件
return (new Pipeline($this->app))
->send($request)
->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
->then($this->dispatchToRouter());
}
//引导数组
protected $bootstrappers = [
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
\Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
\Illuminate\Foundation\Bootstrap\HandleExceptions::class,
\Illuminate\Foundation\Bootstrap\RegisterFacades::class,
\Illuminate\Foundation\Bootstrap\RegisterProviders::class,
\Illuminate\Foundation\Bootstrap\BootProviders::class,
];