Laravel 4 - 何时使用服务提供者?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20439583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:40:55  来源:igfitidea点击:

Laravel 4 - when to use service providers?

laravellaravel-4service-provider

提问by Darius.V

I tried to google it but did not find detailed information.

我试着用谷歌搜索,但没有找到详细信息。

Service providers are a great way to group related IoC registrations in a single location. Think of them as a way to bootstrap components in your application.

服务提供者是将相关 IoC 注册分组到一个位置的好方法。将它们视为在应用程序中引导组件的一种方式。

Not understanding from the documentation. Is this only needed when I create packages? So when I am regular developer and not making some packages to release in public - I don't need to care?

不了解文档。这仅在我创建包时需要吗?因此,当我是常规开发人员并且不制作一些软件包以公开发布时 - 我不需要关心吗?

回答by The Alpha

One of the keys to building a well architected Laravel application is learning to use serviceproviders as an organizational tool. When you are registering many classes with the IoC container, all of those bindings can start to clutter your app/start files. Instead of doing container registrations in those files, create serviceproviders that register related services.

构建架构良好的 Laravel 应用程序的关键之一是学习使用服务提供者作为组织工具。当您向 IoC 容器注册许多类时,所有这些绑定都会开始使您的应用程序/启动文件变得混乱。不是在这些文件中进行容器注册,而是创建注册相关服务的服务提供者。

So, this is a way to organize your application's services in one place to keep it more organized. A service provider must have at least one method: register. The register method is where the provider binds classes to the container. When a request enters your application and the framework is booting up, the register method is called on the providers listed in your configuration file

因此,这是一种将应用程序的服务组织在一个地方以使其更有条理的方法。服务提供者必须至少有一种方法:注册。register 方法是提供者将类绑定到容器的地方。当请求进入您的应用程序并且框架正在启动时,将在您的配置文件中列出的提供程序上调用 register 方法

'providers' => array(
    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    'Illuminate\Cache\CacheServiceProvider',
    // more ...
    'Illuminate\Html\HtmlServiceProvider',
    // more ...
)

This is providers array in app.phpconfig file and this is the HtmlServiceProviderstored in 'Illuminate\Html\HtmlServiceProvider.php'

这是app.php配置文件中的提供者数组,这是HtmlServiceProvider存储在'Illuminate\Html\HtmlServiceProvider.php'

use Illuminate\Support\ServiceProvider;
    class HtmlServiceProvider extends ServiceProvider {

    //...
    public function register()
    {
        $this->registerHtmlBuilder();

        $this->registerFormBuilder();
    }

    protected function registerHtmlBuilder()
    {
        $this->app['html'] = $this->app->share(function($app)
        {
            return new HtmlBuilder($app['url']);
        });
    }

    protected function registerFormBuilder()
    {
        $this->app['form'] = $this->app->share(function($app)
        {
            $form = new FormBuilder($app['html'], $app['url'], $app['session']->getToken());
            return $form->setSessionStore($app['session']);
        });
    }

}

When, Laravelboots up, it calls this (register) method and in this method there are two lines, these line calls two methods, registerHtmlBuilder()and registerFormBuilder(), these both methods components to the IoCcontainer using

时,Laravel启动时,它调用此(register)方法和在该方法中有两条线,这些行调用两种方法,registerHtmlBuilder()并且registerFormBuilder(),这些方法都部件到IoC容器使用

$this->app['html'] = $this->app->share(...);
$this->app['form'] = $this->app->share(...);

In this case both are anonymous functions which returns an instance of html/formclass and that's why, when you use

在这种情况下,两者都是返回html/form类实例的匿名函数,这就是为什么,当您使用

Html::link(...);

Or, using form

或者,使用表单

Form::input(...);

You get the bound class from the $appobject which is available to your application. In this case 'Html' => 'Illuminate\Support\Facades\Html',is used to alias the main class in the aliasesarray in the app.phpfile.

您可以从$app应用程序可用的对象中获取绑定类。在这种情况下'Html' => 'Illuminate\Support\Facades\Html',,用于aliasesapp.php文件中为数组中的主类设置别名。

So, in Laravel, service providers are a way to organize things in a nice cleaner way, during the boot up process of your application, Laravelruns all registermethods from all the service providers so each component become available (bound) to the IoCcontainer so you can access them in your application.

因此,在 中Laravel,服务提供者是一种以更简洁的方式组织事物的方法,在应用程序的启动过程中,Laravel运行register来自所有服务提供者的所有方法,以便每个组件都可用(绑定)到IoC容器,以便您可以访问它们在您的应用程序中。

It's worth mentioning that, after calling of all registermethods from service providers all the bootmethods from those service providers get called. In that case, if you need to use any service from the application (IoC/Service Container) within the service provider class then you should use that service from the boot method since it's not guranteed that any service is avaiable during the registeration of service providers (within register method) because services are registered through registermethod of each service provider but within the bootmethod you may use any service because by then every service is hopefully registered.

值得一提的是,在调用了register服务提供者的所有boot方法之后,这些服务提供者的所有方法都会被调用。在这种情况下,如果您需要在服务提供者类中使用应用程序(IoC/服务容器)中的任何服务,那么您应该从 boot 方法中使用该服务,因为在服务提供者注册期间不保证任何服务都可用(在注册方法中)因为服务是通过register每个服务提供者的boot方法注册的,但是在该方法中您可以使用任何服务,因为届时每个服务都有望被注册。

Check this answer Laravel 4 : How are Facades resolved?too, it may help you to understand.

检查这个答案Laravel 4:Facades 是如何解决的?同样,它可能会帮助您理解。

回答by Andreas

There's nothing a service provider can do that you can't just slap into app/start/global.php, but with a service provider you gather all the logic in one place and can develop application in a more modular fashion.

服务提供者没有什么可以做的,你不能只是进入 app/start/global.php,但是有了服务提供者,你可以在一个地方收集所有的逻辑,并且可以以更加模块化的方式开发应用程序。

If you're making a package a service provider is pretty much a must.

如果您正在制作软件包,则服务提供商几乎是必须的。

回答by Miroslav Trninic

In Laravel, service providers are directly related to the way in which IoC container works. They allow easier and more modular approach to dependencies. In fact, it is a great pattern for organizing your code in a bootstrap fashion ( even outside of Laravel ). I think If you are regular developer you still need to know the basics of Laravel service providers, because that pattern is integral part of Laravel architecture. I am sure it can make your work easier.

在 Laravel 中,服务提供者与 IoC 容器的工作方式直接相关。它们允许使用更简单、更模块化的方法来处理依赖项。事实上,这是一种以引导方式组织代码的好模式(甚至在 Laravel 之外)。我认为如果你是普通开发者,你仍然需要了解 Laravel 服务提供者的基础知识,因为该模式是 Laravel 架构不可或缺的一部分。我相信它可以使您的工作更轻松。

For example, when installing a package, your have to give application access to that package - one of the best solution is through service providers list and a facade. On the other hand, I can't imagine being a Laravel developer without knowing basics of SP-s.

例如,在安装包时,您必须授予应用程序访问该包的权限——最好的解决方案之一是通过服务提供商列表和外观。另一方面,如果不了解 SP-s 的基础知识,我无法想象成为 Laravel 开发人员。