We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在 RouteServiceProvider::boot 阶段,所有路由都只是执行登记,匹配的逻辑是在 02. HTTP Kernel Handle解析 的 dispatchToRouter 阶段。
RouteServiceProvider::boot
我们先找到 RouteServiceProvider.php
RouteServiceProvider.php
在 config/app.php 的 providers 中定义的 RouteServiceProvider 其实是:
config/app.php
providers
laravel/config/app.php
Line 161 in 6d9215c
Illuminate\Foundation\Support\Providers\RouteServiceProvider
laravel/app/Providers/RouteServiceProvider.php
Lines 6 to 9 in 6d9215c
我们分析 Illuminate\Foundation\Support\Providers\RouteServiceProvider 的代码
其 register 方法是空的
register
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php
Lines 81 to 89 in d081c91
而在 boot 阶段,几乎运行了 RouteServiceProvider 中的所有方法
boot
RouteServiceProvider
Lines 24 to 43 in d081c91
Lines 45 to 55 in d081c91
Illuminate\Foundation\Application::routesAreCached, 其实就是判断文件 bootstrap/cache/routes.php 是否存在
Illuminate\Foundation\Application::routesAreCached
bootstrap/cache/routes.php
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
Lines 895 to 903 in d081c91
Lines 905 to 913 in d081c91
Lines 57 to 67 in d081c91
Lines 69 to 79 in d081c91
map
Lines 31 to 43 in d081c91
Lines 45 to 72 in d081c91
mapWebRoutes
mapApiRoutes
Route
Route::group
Illuminate\Routing\Router::group
laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
Lines 356 to 416 in d081c91
Route::get
Route::post
Lines 133 to 214 in d081c91
addRoute
Lines 434 to 445 in d081c91
$this->routes
\Illuminate\Routing\RouteCollection
add
Lines 447 to 478 in d081c91
路由登记完成后,就是 Kernel 触发管道层层剥洋葱调用中间件最后触发路由 dispatch (当然,这已经运行到 RouteServiceProvider 的外面了)。
剥洋葱的过程请查阅 05. Pipeline 解析
Lines 660 to 682 in d081c91
第679行的 $route->run 是至关重要的,调用到了 controller (本质其实是使用 09. 容器的依赖注入机制 将路由方法所依赖参数解析出来,并运行)
$route->run
laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php
Lines 158 to 176 in d081c91
The text was updated successfully, but these errors were encountered:
No branches or pull requests
RouteServiceProvider 详解
我们先找到
RouteServiceProvider.php
在
config/app.php
的providers
中定义的 RouteServiceProvider 其实是:laravel/config/app.php
Line 161 in 6d9215c
而这个类其实继承自
Illuminate\Foundation\Support\Providers\RouteServiceProvider
laravel/app/Providers/RouteServiceProvider.php
Lines 6 to 9 in 6d9215c
我们分析
Illuminate\Foundation\Support\Providers\RouteServiceProvider
的代码register() 阶段
其
register
方法是空的laravel/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php
Lines 81 to 89 in d081c91
boot() 阶段
而在
boot
阶段,几乎运行了RouteServiceProvider
中的所有方法laravel/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php
Lines 24 to 43 in d081c91
一. setRootControllerNamespace()
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php
Lines 45 to 55 in d081c91
二. 判断路由是否缓存过
Illuminate\Foundation\Application::routesAreCached
,其实就是判断文件
bootstrap/cache/routes.php
是否存在laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
Lines 895 to 903 in d081c91
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
Lines 905 to 913 in d081c91
存在则直接加载
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php
Lines 57 to 67 in d081c91
三. 如果没有缓存,则遍历路由
laravel/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php
Lines 69 to 79 in d081c91
map
方法在这里laravel/app/Providers/RouteServiceProvider.php
Lines 31 to 43 in d081c91
laravel/app/Providers/RouteServiceProvider.php
Lines 45 to 72 in d081c91
这里的
mapWebRoutes
和mapApiRoutes
是分别将 routes/web.php 和 routes/api.php 用Route
门面类的 group 加载了一遍。Route::group
实质运行到的是Illuminate\Routing\Router::group
(关于门面类的文章请见 [TODO]),代码为laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
Lines 356 to 416 in d081c91
上面的逻辑是一层层剥开
Route::group
去执行里面的Route::get
/Route::post
...laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
Lines 133 to 214 in d081c91
不难发现,不管是 GET/POST... 还是 any,都只是调用了
addRoute
将这个路由的属性登记了一下:laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
Lines 434 to 445 in d081c91
$this->routes
是一个\Illuminate\Routing\RouteCollection
的集合类,add
由这个方法生成的路由laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
Lines 447 to 478 in d081c91
四. dispatch() -> runRoute() 阶段
路由登记完成后,就是 Kernel 触发管道层层剥洋葱调用中间件最后触发路由 dispatch (当然,这已经运行到 RouteServiceProvider 的外面了)。
laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
Lines 660 to 682 in d081c91
第679行的
$route->run
是至关重要的,调用到了 controller (本质其实是使用 09. 容器的依赖注入机制 将路由方法所依赖参数解析出来,并运行)laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php
Lines 158 to 176 in d081c91
The text was updated successfully, but these errors were encountered: