一、服务容器的注册
E:\xampp\htdocs\xampp\laravel58\vendor\laravel\framework\src\Illuminate\Container\Container.php
确定给定的抽象类型是否已绑定。 bound
确定给定字符串是否为别名 isAlias
在容器中注册共享绑定 bind
容器的共享实例 instances
容器绑定 bindings
将现有实例注册为容器中的共享实例 instance
由抽象名称键入的注册别名 abstractAliases
获取Laravel安装的基本路径。
获取缓存packages.php文件的路径。
为HTTP请求引导应用程序 bootstrap
管道 Pipeline
注册所有配置的提供程序 registerConfiguredProviders
将路由绑定到给定的执行请求 bind的方法 src\Illuminate\Routing\Route.php
func_num_args
这个是根据接口的实现找到对应的类 就像一个接口 可以被多个类实现 那么运行的时候的时候,看到这个接口就直接找到对应的类就可以了 就是契约拿东西 就是下面的代码
Illuminate\Contracts\Http\Kernel::class
E:\xampp\htdocs\xampp\laravel58\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php下的 protected $bootstrappers都解析出来
系统的应用服务
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,
];
加载配置文件 路径在上面
\Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
#加载的 config.php\app.php\providers属性
\Illuminate\Foundation\Bootstrap\RegisterProviders::class,
先跳到E:\xampp\htdocs\xampp\laravel58\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\RegisterProviders.php
在跳到
E:\xampp\htdocs\xampp\laravel58\vendor\laravel\framework\src\Illuminate\Foundation\Application.php\registerConfiguredProviders方法
#服务容器的注册
/**
* Register all of the configured providers.
*
* @return void
*/
public function registerConfiguredProviders()
{
$providers = Collection::make($this->config['app.providers'])
->partition(function ($provider) {
return Str::startsWith($provider, 'Illuminate\\');
});
$providers->splice(1, 0, [$this->make(PackageManifest::class)->providers()]);
(new ProviderRepository($this, new Filesystem, $this->getCachedServicesPath()))
->load($providers->collapse()->toArray());
}
E:\xampp\htdocs\xampp\laravel58\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php \sendRequestThroughRouter的方法
/**
* 通过中间件/路由器发送给定的请求。
* Send the given request through the middleware / router.
*
* @param \Illuminate\Http\Request $request 请求
* @return \Illuminate\Http\Response 返回
*/
#通过中间件/路由器发送给定的请求。
protected function sendRequestThroughRouter($request)
{
#绑定公共的应用实例
$this->app->instance('request', $request);
#清空门脸的实例(别名)
Facade::clearResolvedInstance('request');
#引导laravel加载系统的所需要的服务,包含自定义的服务,主要通过在$bootstrappers属性中定义的服务器容器引导
$this->bootstrap(); #服务器容器在这步骤注册的
#中间件实在这加载,以管道形式加载 $this->middleware
#then($this->dispatchToRouter() 路由请求的分发---获取路由调度器回调
return (new Pipeline($this->app))
->send($request)
->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
->then($this->dispatchToRouter());
}
E:\xampp\htdocs\xampp\laravel58\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\LoadConfiguration.php
二、路由的解析
E:\xampp\htdocs\xampp\laravel58\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php
/**
* Get the route dispatcher callback.
*
* @return \Closure
*/
protected function dispatchToRouter()
{
return function ($request) {
$this->app->instance('request', $request);
return $this->router->dispatch($request);
};
}
E:\xampp\htdocs\xampp\laravel58\vendor\laravel\framework\src\Illuminate\Routing\Router.php
/**
* Dispatch the request to the application. 将请求发送到应用程序
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*/
public function dispatch(Request $request)
{
$this->currentRequest = $request;
return $this->dispatchToRoute($request);
}
/**
* Dispatch the request to a route and return the response.
*
* @param \Illuminate\Http\Request $request
* @return mixed
*/
public function dispatchToRoute(Request $request)
{
#runRoute运行路由请求--实际用户请求 执行
#findRoute查找路由请求
return $this->runRoute($request, $this->findRoute($request));
}
/**
* Find the route matching a given request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Routing\Route
*/
#查找并且匹配请求对应的路由
protected function findRoute($request)
{
$this->current = $route = $this->routes->match($request);
$this->container->instance(Route::class, $route);
return $route;
}
E:\xampp\htdocs\xampp\laravel58\vendor\laravel\framework\src\Illuminate\Routing\RouteConllection.php
/**
* Find the first route matching a given request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Routing\Route
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
#根据用户的请求然后,获取对应请求路由
#检验用户的请求
#最后返回对应的实例,有可能是闭包
public function match(Request $request)
{
//获取用户的请求类型,然后根据请求类型选择对应的路由
$routes = $this->get($request->getMethod());
// First, we will see if we can find a matching route for this current request
// method. If we can, great, we can just return it so that it can be called
// by the consumer. Otherwise we will check for routes with another verb.
#匹配路由
$route = $this->matchAgainstRoutes($routes, $request);
#参数不是null
if (! is_null($route)) {
#并返回路由的实例,并将路由绑定到给定的执行请求
return $route->bind($request);
}
// If no route was found we will now check if a matching route is specified by
// another HTTP verb. If it is we will need to throw a MethodNotAllowed and
// inform the user agent of which HTTP verb it should use for this route.
$others = $this->checkForAlternateVerbs($request);
if (count($others) > 0) {
return $this->getRouteForMethods($request, $others);
}
throw new NotFoundHttpException;
}
启动laravel就会调用public/index.php
1. 先加载require __DIR__.'/../vendor/autoload.php';
2. 在加载 $app = require_once __DIR__.'/../bootstrap/app.php';
3. 在去执行下面的内容
#Illuminate\Contracts\Http\Kernel::class 解析的是app/Http/kernel.php这个文件
#获取容器使用make,用这个字符串获取出他的对象
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle( //执行用户的请求
$request = Illuminate\Http\Request::capture() #获取用户请求参数
);
$response->send(); //输出结果
$kernel->terminate($request, $response); //停止用户所开启的中间件
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,
];
match校验万了以后,在路由所定义的内容 红色的部分,可能死地址可能是闭包
Route::get(’/’, function () {
return view(‘welcome’);
});
运行的路由
E:\xampp\htdocs\xampp\blog\vendor\laravel\framework\src\Illuminate\Routing\Router.php\runRoute的方法
运行的路由 ,根据给的路由,去请求结果
protected function runRoute(Request $request, Route $route)
{
$request->setRouteResolver(function () use ($route) {
return $route;
});
// 请求前的事件
$this->events->dispatch(new Events\RouteMatched($route, $request));
//prepareResponse个方法是响应出,这里执行 $this->runRouteWithinStack($route, $request)控制器的方法和路由中定义闭包的内容
return $this->prepareResponse($request,
$this->runRouteWithinStack($route, $request)
);//返回的对象是结果集
}
E:\xampp\htdocs\xampp\blog\vendor\laravel\framework\src\Illuminate\Routing\Router.php\runRouteWithinStack的方法
路由的开始
$route->run() 才是重要的部分 是执行的
protected function runRouteWithinStack(Route $route, Request $request)
{
//判断是否开启控制器中间件
$shouldSkipMiddleware = $this->container->bound('middleware.disable') &&
$this->container->make('middleware.disable') === true;
//有值并且等于true 返回 两个部分的中间件 一部分是web 和 自定义创建的
//app\http\middleware 的 web 中间件 和 $this->middleware() 自定义创建的
$middleware = $shouldSkipMiddleware ? [] : $this->gatherRouteMiddleware($route);
#进入管道模式完成操作 中间件放到管道模式完成
return (new Pipeline($this->container))
->send($request)
->through($middleware)
->then(function ($request) use ($route) {
return $this->prepareResponse(
$request, $route->run()
);
});
}
什么时候注入的参数在这里面
use RouteDependencyResolverTrait; //控制器的传的参数要看这个方法