Laravel 4-何时使用服务提供商? [英] Laravel 4 - when to use service providers?
问题描述
我尝试用Google搜索它,但没有找到详细信息.
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(
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
'Illuminate\Auth\AuthServiceProvider',
'Illuminate\Cache\CacheServiceProvider',
// more ...
'Illuminate\Html\HtmlServiceProvider',
// more ...
)
这是app.php
配置文件中的providers数组,这是存储在'Illuminate\Html\HtmlServiceProvider.php'
This is providers array in app.php
config file and this is the HtmlServiceProvider
stored in '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']);
});
}
}
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' => 'Illuminate\Support\Facades\Html',
用作app.php
文件中aliases
数组中主类的别名.
You get the bound class from the $app
object which is available to your application. In this case 'Html' => 'Illuminate\Support\Facades\Html',
is used to alias the main class in the aliases
array in the app.php
file.
因此,在Laravel
中,服务提供者是一种以一种更简洁的方式组织事物的方式,在应用程序的启动过程中,Laravel
运行来自所有服务提供者的所有register
方法,因此每个组件变得对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:如何解决立面? a>也可以帮助您理解.
Check this answer Laravel 4 : How are Facades resolved? too, it may help you to understand.
这篇关于Laravel 4-何时使用服务提供商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!