服务提供者

简介

Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services are bootstrapped via service providers.

服务提供者是所有Laravel应用程序的启动中心, 你的应用程序,和所有Laravel的核心服务,都通过服务提供者来启动。

But, what do we mean by "bootstrapped"? In general, we mean registering things, including registering service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application.

但是我们说的“启动”?一般而言,我们是指注册事物, 包括注册服务容器绑定, 事件监听器,中间件甚至路由, 服务提供者是你应用程序配置中心所在。

 

If you open the config/app.php file included with Laravel, you will see a providers array. These are all of the service provider classes that will be loaded for your application. Of course, many of them are "deferred" providers, meaning they will not be loaded on every request, but only when the services they provide are actually needed.

如果你打开包含于Laravel中的 config/app.php 这一文件, 你会看到providers数组, 他们是所有将要加载到你的应用程序的服务提供者类。 当然,他们之中有很多属于“缓载”提供者, 意思是除非真正需要他们所提供的服务, 否则它们并不在每个请求中都被加载。

In this overview you will learn how to write your own service providers and register them with your Laravel application.

这份概述中,你将会学到如何编写你自己的服务提供者, 并把他们注册于你的Laravel应用程序。

#编写服务提供者

All service providers extend the Illuminate\Support\ServiceProvider class. This abstract class requires that you define at least one method on your provider: register. Within the registermethod, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the registermethod.

所有服务提供者都继承 Illuminate\Support\ServiceProvider  类, 这个抽象类要求你至少实现一个register 方法, 在这个 register方法中, 你应该只绑定到服务容器里的东西, 你不应该尝试注册任何事件监听器, 路由,和其他功能。

The Artisan CLI can easily generate a new provider via the make:provider command:

Artisan CLI 很容易通过 make: provider命令 生成一个新的服务提供者

php artisan make:provider RiakServiceProvider

Register方法

As mentioned previously, within the register method, you should only bind things into theservice container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method. Otherwise, you may accidently use a service that is provided by a service provider which has not loaded yet.

Now, let's take a look at a basic service provider:

就像前面提到过的, 在register 方法里, 你只绑定到服务容器之中, , 你绝不应该视图注册任何的事件监听器,路由,和其他功能, 否则,你可能意外的使用的一个服务提供者提供的服务,而这个服务还没有被加载。

<?php

namespace App\Providers;

use Riak\Connection;
use Illuminate\Support\ServiceProvider; class RiakServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
$this->app->singleton('Riak\Contracts\Connection', function ($app) {
return new Connection(config('riak'));
});
}
}

This service provider only defines a register method, and uses that method to define an implementation of Riak\Contracts\Connection in the service container. If you don't understand how the service container works, check out its documentation.

这个务提供者定义了一个register方法,是用这个方法在服务容器中定义了一个Riak\Contracts\Connection 的实现, 如果你不懂服务容器的工作原理,我们很快就会提到

启动方法 Boot Method

So, what if we need to register a view composer within our service provider? This should be done within the boot method. This method is called after all other service providers have been registered, meaning you have access to all other services that have been registered by the framework:

那么,我们需要注册一个view composer 在我们服务提供者,该怎么做,这就要在boot方法中写, 这个方法会在所有服务提供者注册后被调用,这能让你取得所有其他已经注册的服务。

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
view()->composer('view', function () {
//
});
} /**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
//
}
}

启动方法依赖注入

We are able to type-hint dependencies for our boot method. The service container will automatically inject any dependencies you need:

我们能够类型提示 boot方法的依赖, 服务容器将会自动注入任何的需要的依赖。

use Illuminate\Contracts\Routing\ResponseFactory;

public function boot(ResponseFactory $factory)
{
$factory->macro('caps', function ($value) {
//
});
}

#注册提供者

All service providers are registered in the config/app.php configuration file. This file contains a providers array where you can list the names of your service providers. By default, a set of Laravel core service providers are listed in this array. These providers bootstrap the core Laravel components, such as the mailer, queue, cache, and others.

所有的服务提供者都在 config/app.php 配置文件中被注册。 此文件包含了一个providers 数组, 你可以在其中列出所有服务提供者的名称。 此数组会默认列出一组Laravel核心服务提供者, 这些提供者启动了Laravel的核心组件, 例如邮件发送者,队列,缓存及其他等等。

To register your provider, simply add it to the array:

要注册你的提供者,只要把它加入次数组。

'providers' => [
// Other Service Providers 'App\Providers\AppServiceProvider',
],

#缓载提供者

If your provider is only registering bindings in the service container, you may choose to defer its registration until one of the registered bindings is actually needed. Deferring the loading of such a provider will improve the performance of your application, since it is not loaded from the filesystem on every request.

若你的提供者仅仅用于绑定注册到服务容器, 你可以选择延缓其注册, 知道真正需要其中注册的绑定才加载。 延缓像这样的一个提供者加载可以增进应用程序的性能,因为这样就不用每个请求都从文件系统中将其加载。

To defer the loading of a provider, set the defer property to true and define a providesmethod. The provides method returns the service container bindings that the provider registers:

要延缓提供者加载,将defer性质设为true, 并定义一个provides方法。 provides方法应返回提供者所注册的服务容器绑定。

<?php

namespace App\Providers;

use Riak\Connection;
use Illuminate\Support\ServiceProvider; class RiakServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true; /**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('Riak\Contracts\Connection', function ($app) {
return new Connection($app['config']['riak']);
});
} /**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['Riak\Contracts\Connection'];
} }

Laravel compiles and stores a list of all of the services supplied by deferred service providers, along with the name of its service provider class. Then, only when you attempt to resolve one of these services does Laravel load the service provider.

Laravel 编译并保存所有由延缓服务提供者所提供的服务清单, 以及其服务者的类名称。 只有在当你尝试解析其中的服务时,Laravel才会加载服务提供者。

Laravel5.1学习笔记11 系统架构3 服务提供者的更多相关文章

  1. Laravel5.1学习笔记i14 系统架构6 Facade

    Facades 介绍  使用 Facades Facade 类参考   #介绍 Facades provide a "static" interface to classes th ...

  2. Laravel5.1学习笔记12 系统架构4 服务容器

    Service Container 介绍 绑定的用法  绑定实例到接口 上下文绑定 标签 解析 容器事件 #介绍 The Laravel service container is a powerful ...

  3. Laravel5.1学习笔记13 系统架构5 Contract

    Contract 简介 为什么要用 Contract? Contract 参考 如何使用 Contract 简介 Laravel 中的 Contract 是一组定义了框架核心服务的接口.例如,Illu ...

  4. Laravel5.1学习笔记10 系统架构2 应用程序结构

    应用程序结构 简介 根目录 App 目录 为应用程序设置命名空间 简介 默认的 Laravel 应用程序结构是为了给无论构建大型还是小型应用程序都提供一个良好的开始.当然,你可以依照喜好自由地组织应用 ...

  5. Laravel5.1学习笔记9 系统架构1 请求生命周期 (待修)

    Request Lifecycle Introduction Lifecycle Overview Focus On Service Providers Introduction When using ...

  6. ODI学习笔记2--ODI产品架构

    ODI学习笔记2--ODI产品架构 ODI产品架构: ODI提供了以下几种管理工具:Designer 用于定义数据转换逻辑,这是最常用的开发工具,大部分的开发任务,包括data store的定义,in ...

  7. SQL反模式学习笔记11 限定列的有效值

    目标:限定列的有效值,将一列的有效字段值约束在一个固定的集合中.类似于数据字典. 反模式:在列定义上指定可选值 1. 对某一列定义一个检查约束项,这个约束不允许往列中插入或者更新任何会导致约束失败的值 ...

  8. 《C++ Primer Plus》学习笔记11

    <C++ Primer Plus>学习笔记11 第17章 输入.输出和文件 <<<<<<<<<<<<<< ...

  9. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

随机推荐

  1. Thinkphp 批量更新方法 saveALL

    批量更新只适用于一个字段的更新,原理是用自定义函数拼接sql语句,然后再执行sql语句. //数据 $data[] = array('id'=>1,'value'=>value1); $d ...

  2. 《阿里巴巴Java开发手册》更新为《Java开发手册》

    新版一览:华山版<Java开发手册> <阿里巴巴Java开发手册>始于阿里内部规约,在全球Java开发者共同努力下,已成为业界普遍遵循的开发规范,涵盖编程规约.异常日志.单元测 ...

  3. 【03】AngularJS 简介

    AngularJS 简介 AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 HTML 页面. AngularJS 通过 指令 扩展了 HTML ...

  4. 【02】Node.js 安装配置(OK)

    [02] Node.js 安装配置 本章节我们将向大家介绍在window和Linux上安装Node.js的方法. Node.js安装包及源码下载地址为:http://www.nodejs.org/do ...

  5. hdu 2224 双调欧几里得旅行商问题tsp

    /* 题意:平面上n个点,确定一条连接各点的最短闭合旅程且每个点仅用一次.这个解的一般形式为NP的(在多项式时间内可以求出) 建议通过只考虑双调旅程(bitonictour)来简化问题,这种旅程即为从 ...

  6. Nth Largest Value

    Problem Description For this problem, you will write a program that prints the Nth largest value in ...

  7. spring mvc参数校验

    一.在SringMVC中使用 使用注解 1.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-validator-4.2.0.Fina ...

  8. BP神经网络及其在教学质量评价中 的应用

    本文学习笔记是自己的理解,如有错误的地方,请大家指正批评.共同进步.谢谢! 之前的教学质量评价,仅仅是通过对教学指标的简单处理.如求平均值或人为的给出各指标的权值来加权求和,其评价结果带有非常大主观性 ...

  9. Linux网络编程:UDP实现可靠的文件传输

    我们知道,用TCP实现文件传输很简单.相对于TCP,因为UDP是面向无连接.不可靠的传输协议,所以我们需要考虑丢包和后发先至(包的顺序)的问题,所以我们想要实现UDP传输文件,则需要解决这两个问题.方 ...

  10. C#邮件发送(含附件)

    class SendEmail { static void Main(string[] args) { string from = "发件人@yingu.com"; string ...