簡短的回答:
您編譯清單文件已經被編譯框架。
在其寫入名爲緩存文件中的第一次,當Laravel構建應用程序(和解決所有的服務提供商在IOC) services.php(即清單文件,放置於:bootstrap/cache/services.php
)。
因此,如果您清除通過php artisan clear-compiled
命令編譯它應該迫使框架重建清單文件,你可以注意的是provides
方法被調用。 在接下來的調用/請求provides
方法不再被調用。
框架啓動的順序幾乎是這樣的:
//public/index.php
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
\Illuminate\Foundation\Http\Kernel::__construct();
\Illuminate\Foundation\Http\Kernel::handle();
\Illuminate\Foundation\Http\Kernel::sendRequestThroughRouter();
\Illuminate\Foundation\Http\Kernel::bootstrap();
\Illuminate\Foundation\Application::bootstrapWith();
# where $bootstrapper is a item from \Illuminate\Foundation\Http\Kernel::$bootstrappers
# and $this is instance of \Illuminate\Foundation\Application
\Illuminate\Foundation\Application::make($bootstrapper)->bootstrap($this);
一個bootstrappers的是Illuminate\Foundation\Bootstrap\RegisterProviders
其中 調用\Illuminate\Foundation\Application::registerConfiguredProviders()
,然後調用 \Illuminate\Foundation\ProviderRepository::__construct()
最後:
\Illuminate\Foundation\ProviderRepository::load()
當\Illuminate\Foundation\ProviderRepository::load()
是召集所有服務供應商註冊和 \Illuminate\Support\ServiceProvider::provides()
也被稱爲好。
這裏是你應該知道(從\Illuminate\Foundation\ProviderRepository::load
)的片段:
/**
* Register the application service providers.
*
* @param array $providers
* @return void
*/
public function load(array $providers)
{
$manifest = $this->loadManifest();
// First we will load the service manifest, which contains information on all
// service providers registered with the application and which services it
// provides. This is used to know which services are "deferred" loaders.
if ($this->shouldRecompile($manifest, $providers)) {
$manifest = $this->compileManifest($providers);
}
// Next, we will register events to load the providers for each of the events
// that it has requested. This allows the service provider to defer itself
// while still getting automatically loaded when a certain event occurs.
foreach ($manifest['when'] as $provider => $events) {
$this->registerLoadEvents($provider, $events);
}
// We will go ahead and register all of the eagerly loaded providers with the
// application so their services can be registered with the application as
// a provided service. Then we will set the deferred service list on it.
foreach ($manifest['eager'] as $provider) {
$this->app->register($this->createProvider($provider));
}
$this->app->addDeferredServices($manifest['deferred']);
}
\Illuminate\Foundation\ProviderRepository::compileManifest()
是執行你的provides()
方法的地方。
您是否在'config/app.php'中的provider數組中註冊了提供程序? – user3158900
@ user3158900是的。提供程序工作正常,但提供()不會被調用,所以我不認爲它正常推遲。 – Julian
只要確保因爲Laravel的容器實際上可以創建類的實例,而不需要他們在服務提供者中註冊。所以如果Laravel不知道服務提供商,那麼它不知道它需要推遲它。 – user3158900