laravel8脚手架引起的关于ServiceProvider执行顺序的疑惑,请大佬能帮忙解惑下,谢谢
初衷
我想自定义实现用户的登录认证,原来使用的laravel脚手架程序(laravel\fortify)
步骤
首先移除脚手架自带的路由,从 \vendor\laravel\fortify\src\FortifyServiceProvider.php 中发现如下代码:
class FortifyServiceProvider extends ServiceProvider
{
...
public function boot()
{
$this->configurePublishing();
$this->configureRoutes();
}
...
protected function configureRoutes()
{
if (Fortify::$registersRoutes) {
Route::group([
'namespace' => 'Laravel\Fortify\Http\Controllers',
'domain' => config('fortify.domain', null),
'prefix' => config('fortify.path'),
], function () {
$this->loadRoutesFrom(__DIR__.'/../routes/routes.php');
});
}
}
}
在\vendor\laravel\fortify\src\Fortify.php 类中有如下代码
class Fortify
{
...
public static $registersRoutes = true;
...
public static function ignoreRoutes()
{
static::$registersRoutes = false;
return new static;
}
}
所以我在App\Providers\AppServiceProvider 中调用 ignoreRoutes()方法,如下:
class AppServiceProvider extends ServiceProvider
{
...
public function boot()
{
Fortify::ignoreRoutes();
}
}
执行结果:脚手架的路由依旧生效,打印跟踪发现先执行的 FortifyServiceProvider 类中的 boot 方法,然后才执行的 AppServiceProvider 中的 boot 方法,即在我忽略路由的时候,路由已经注册好了。
问题
1.这是脚手架程序的BUG还是我调用 Fortify::ignoreRoutes() 的位置不对。
2.laravel中多个 ServiceProvider 中的 boot() 方法有没执行顺序,或者哪个是最先执行
感谢大佬帮忙答疑
关于 LearnKu
推荐文章: