返回介绍

Service Provider

发布于 2021-11-04 22:44:46 字数 3885 浏览 904 评论 0 收藏 0

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. The most common use case for service providers is to create, modify, or replace container bindings. That being said, service providers allow you full access to run whatever logic you need during application boot with access to the container.

Advanced Use Only!!!

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.

To understand service providers, you must first understand the order in which Flarum boots. Most of this happens in Flarum\Foundation\InstalledSite

  1. The container and application are initialized, and essential bindings (config, environment, logger) are registered
  2. The register methods of all core service providers are run.
  3. The extend methods of all extenders used by all enabled extensions are run.
  4. The extend methods of all extenders used in the Flarum site's local extend.php are run.
  5. The boot methods of all core service providers are run.

A custom service provider should extend Flarum\Foundation\AbstractServiceProvider, and can have a boot and a register method. 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.

To actually register your custom service provider, you can use the ServiceProvider extender in extend.php:


<?php


use Flarum\Extend;


return [
    // Other extenders
    (new Extend\ServiceProvider())
        ->register(CustomServiceProvider::class),
    // Other extenders
];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文