服务提供者
As noted throughout this documentation, Flarum uses Laravel's service container (or IoC container) for dependency injection. Service Providers allow low-level configuration and modification of the Flarum backend. 服务提供者最常用的情况是创建、修改或替换容器绑定。尽管如此,服务提供者允许您在应用程序启动时完全访问任何您需要的逻辑,并且访问容器。
Unlike with other extenders, the Service Provider layer is NOT use-case driven, and is NOT considered public API. It is subject to change at any time, without notice or deprecation. This should only be used if you know what you're doing, and the other extenders don't satisfy your use case. 它可以随时更改,无须通知或弃权。只有当您知道自己在做什么,而其他扩展程序不能满足您的使用需要时,才能使用这个方法。
Flarum 启动过程
要了解服务提供商,您必须首先了解Flarum 引导的顺序。 Most of this happens in Flarum\Foundation\InstalledSite
- 容器和应用程序已初始化,基本绑定(config, environment, logger) 已注册
- The
registermethods of all core service providers are run. - The
extendmethods of all extenders used by all enabled extensions are run. - The
extendmethods of all extenders used in the Flarum site's localextend.phpare run. - The
bootmethods of all core service providers are run. - The
Flarum\Foundation\Event\ApplicationBootedevent is fired. See backend events for details.
自定义服务提供商
A custom service provider should extend Flarum\Foundation\AbstractServiceProvider, and can have a boot and a register method. Third-party Laravel packages that ship their own service providers (extending Illuminate\Support\ServiceProvider) can also be registered directly — see below.
For example:
<?php
use Flarum\Foundation\AbstractServiceProvider;
use Illuminate\Contracts\Container\Container;
class CustomServiceProvider extends AbstractServiceProvider
{
public function register()
{
// custom logic here, for example:
$this->container->resolving(SomeClass::class, function ($container) {
return new SomeClass($container->make('some.binding'));
});
}
public function boot(Container $container)
{
// custom logic here
}
}
The register method will run during step (3) above, and the boot method will run during step (5) above. In the register method, the container is available via $this->container. In the boot method, the container (or any other arguments), should be injected via typehinted method arguments.
Flarum does not currently support Laravel Octane, but some best practices, like using the $container argument inside bind, singleton, and resolving callbacks instead of $this->container should be used. See the Octane documentation for more information.
Registering a Service Provider
To register a service provider, use the ServiceProvider extender in extend.php:
<?php
use Flarum\Extend;
return [
// Other extenders
(new Extend\ServiceProvider())
->register(CustomServiceProvider::class),
// Other extenders
];
The register method accepts any class that extends either Flarum\Foundation\AbstractServiceProvider or Illuminate\Support\ServiceProvider. This means you can directly register service providers shipped by third-party Laravel packages without wrapping them:
use Flarum\Extend;
use SomePackage\SomePackageServiceProvider;
return [
(new Extend\ServiceProvider())
->register(SomePackageServiceProvider::class),
];