laravel RouteServiceProvider注册和引导过程(另一部分)

protected $bootstrappers = [
    ....
    \Illuminate\Foundation\Bootstrap\RegisterProviders::class, 
    \Illuminate\Foundation\Bootstrap\BootProviders::class,
];

分别对应:

1. $app->registerConfiguredProviders();

通过ProviderRepository实例的load方法调用$this->app->register($provider);

public function register($provider, $force = false)
{
    ....
    $provider->register();
    ....
    if ($this->isBooted()) {//默认false,猜测此处在注册reffer providers时为true
        $this->bootProvider($provider);
    }

    return $provider;
}
//此处$this指向RouteServiceProvider
public function register()
{
    //赋值$this->bootedCallbacks
    $this->booted(function () {
        $this->setRootControllerNamespace();

        if ($this->routesAreCached()) {
            $this->loadCachedRoutes();
        } else {
            $this->loadRoutes();

            $this->app->booted(function () {
                $this->app['router']->getRoutes()->refreshNameLookups();
                $this->app['router']->getRoutes()->refreshActionLookups();
            });
        }
    });    
}

2. $app->boot();

public function boot()
{
    if ($this->isBooted()) {
        return;
    }
    
    $this->fireAppCallbacks($this->bootingCallbacks);

    array_walk($this->serviceProviders, function ($p) {
        $this->bootProvider($p);
    });

    $this->booted = true;

    $this->fireAppCallbacks($this->bootedCallbacks);
}
protected function bootProvider(ServiceProvider $provider)
{
    $provider->callBootingCallbacks();

    if (method_exists($provider, 'boot')) {
        $this->call([$provider, 'boot']);
    }

    $provider->callBootedCallbacks();
}
//此处$this指向RouteServiceProvider
public function boot(): void
{
    RateLimiter::for('api', function (Request $request) {
        return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
    });

    //赋值$this->loadRoutesUsing
    $this->routes(function () {
        Route::middleware('api')
            ->prefix('api')
            ->group(base_path('routes/api.php'));

        Route::middleware('web')
            ->group(base_path('routes/web.php'));
    });
}
function () {
    $this->setRootControllerNamespace();

    if ($this->routesAreCached()) {
        $this->loadCachedRoutes();
    } else {
        $this->loadRoutes();

        $this->app->booted(function () {
            $this->app['router']->getRoutes()->refreshNameLookups();
            $this->app['router']->getRoutes()->refreshActionLookups();
        });
    }
});  
protected function loadRoutes()
{
    if (! is_null($this->loadRoutesUsing)) {
        $this->app->call($this->loadRoutesUsing);
    } elseif (method_exists($this, 'map')) {
        $this->app->call([$this, 'map']);
    }
}

以上说明,Route::middleware('web')->group(base_path('routes/web.php'));

是在provider booted之后,app booted之前执行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值