Laravel 4 - 何时使用服务提供者? [英] Laravel 4 - when to use service providers?

查看:21
本文介绍了Laravel 4 - 何时使用服务提供者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

从文档中无法理解.这仅在我创建包时需要吗?因此,当我是普通开发人员并且不制作一些要公开发布的软件包时 - 我不需要关心?

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?

推荐答案

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

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.

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

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

'providers' => array(
    'IlluminateFoundationProvidersArtisanServiceProvider',
    'IlluminateAuthAuthServiceProvider',
    'IlluminateCacheCacheServiceProvider',
    // more ...
    'IlluminateHtmlHtmlServiceProvider',
    // more ...
)

这是 app.php 配置文件中的 providers 数组,这是存储在 'IlluminateHtmlHtmlServiceProvider.php' 中的 HtmlServiceProvider

This is providers array in app.php config file and this is the HtmlServiceProvider stored in 'IlluminateHtmlHtmlServiceProvider.php'

use IlluminateSupportServiceProvider;
    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']);
        });
    }

}

Laravel 启动时,它会调用这个 (register) 方法,在这个方法中有两行,这行调用了两个方法,registerHtmlBuilder()registerFormBuilder(),这两个方法组件到 IoC 容器使用

When, Laravel boots 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 IoC container using

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

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

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

Html::link(...);

或者,使用表单

Form::input(...);

您从应用程序可用的 $app 对象获取绑定类.在这种情况下 'Html' =>'IlluminateSupportFacadesHtml', 用于在 app.php 文件中的 aliases 数组中为主类起别名.

You get the bound class from the $app object which is available to your application. In this case 'Html' => 'IlluminateSupportFacadesHtml', is used to alias the main class in the aliases array in the app.php file.

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

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

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

It's worth mentioning that, after calling of all register methods from service providers all the boot methods 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 register method of each service provider but within the boot method you may use any service because by then every service is hopefully registered.

检查这个答案Laravel 4:Facades 是如何解决的? 也可以帮助你理解.

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

这篇关于Laravel 4 - 何时使用服务提供者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆