Table of Contents
1、简介
2、 绑定
绑定一个单例
绑定实例
绑定接口到实现
上下文绑定
标签
3、解析
4、容器事件
Home Backend Development PHP Tutorial [ Laravel 5.2 文档 ] 架构 -- 服务容器

[ Laravel 5.2 文档 ] 架构 -- 服务容器

Jun 20, 2016 pm 12:41 PM

1、简介

Laravel服务容器是一个用于管理类依赖和执行依赖注入的强大工具。依赖注入听上去很花哨,其实质是通过构造函数或者某些情况下通过 set方法将类依赖注入到类中。

让我们看一个简单的例子:

<?phpnamespace App\Jobs;use App\User;use Illuminate\Contracts\Mail\Mailer;use Illuminate\Contracts\Bus\SelfHandling;class PurchasePodcast implements SelfHandling{    /**     * 邮件实现     */    protected $mailer;    /**     * 创建一个新的实例     *     * @param  Mailer  $mailer     * @return void     */    public function __construct(Mailer $mailer)    {        $this->mailer = $mailer;    }    /**     * 购买一个播客     *     * @return void     */    public function handle()    {        //    }}
Copy after login

在本例中,当播客被购买后 PurchasePodcast任务需要发送邮件,因此,你需要注入一个可以发送邮件的服务。由于该服务是被注入的,我们可以方便的使用其另一个实现来替换它,在测试的时候我们还可以“模拟”或创建一个假的邮件实现。

深入理解 Laravel 服务容器对于构建功能强大的大型 Laravel 应用而言至关重要,对于贡献代码到 Laravel 核心也很有帮助。

2、 绑定

几乎所有的服务容器绑定都是在服务提供者中完成。因此本章节的演示例子用到的容器都是在这种上下文环境中,如果一个类没有基于任何接口那么就没有必要将其绑定到容器。容器并不需要被告知如何构建对象,因为它会使用 PHP 的反射服务自动解析出具体的对象。

在一个服务提供者中,可以通过 $this->app变量访问容器,然后使用 bind方法注册一个绑定,该方法需要两个参数,第一个参数是我们想要注册的类名或接口名称,第二个参数是返回类的实例的闭包:

$this->app->bind('HelpSpot\API', function ($app) {    return new HelpSpot\API($app['HttpClient']);});
Copy after login

注意到我们接受容器本身作为解析器的一个参数,然后我们可以使用该容器来解析我们正在构建的对象的子依赖。

绑定一个单例

singleton方法绑定一个只需要解析一次的类或接口到容器,然后接下来对容器的调用将会返回同一个实例:

$this->app->singleton('FooBar', function ($app) {    return new FooBar($app['SomethingElse']);});
Copy after login

绑定实例

你还可以使用 instance方法绑定一个已存在的对象实例到容器,随后对容器的调用将总是返回给定的实例:

$fooBar = new FooBar(new SomethingElse);$this->app->instance('FooBar', $fooBar);
Copy after login

绑定接口到实现

服务容器的一个非常强大的特性是其绑定接口到实现的能力。我们假设有一个 EventPusher接口及其 RedisEventPusher实现,编写完该接口的 RedisEventPusher实现后,就可以将其注册到服务容器:

$this->app->bind('App\Contracts\EventPusher', 'App\Services\RedisEventPusher');
Copy after login

这段代码告诉容器当一个类需要 EventPusher的实现时将会注入 RedisEventPusher,现在我们可以在构造器或者任何其它通过服务容器注入依赖的地方进行 EventPusher接口的类型提示:

use App\Contracts\EventPusher;/** * 创建一个新的类实例 * * @param  EventPusher  $pusher * @return void */public function __construct(EventPusher $pusher){    $this->pusher = $pusher;}
Copy after login

上下文绑定

有时侯我们可能有两个类使用同一个接口,但我们希望在每个类中注入不同实现,例如,当系统接到一个新的订单的时候,我们想要通过 PubNub而不是 Pusher 发送一个事件。Laravel 定义了一个简单、平滑的方式来定义这种行为:

$this->app->when('App\Handlers\Commands\CreateOrderHandler')          ->needs('App\Contracts\EventPusher')          ->give('App\Services\PubNubEventPusher');
Copy after login

你甚至还可以传递一个闭包到 give方法:

$this->app->when('App\Handlers\Commands\CreateOrderHandler')    ->needs('App\Contracts\EventPusher')    ->give(function () {        // Resolve dependency...    });
Copy after login

绑定原始值

有时候你可能有一个获取若干注入类的类,但还需要一个注入的原始值,比如整型数据,你可以轻松使用上下文绑定来注入指定类所需要的任何值:

$this->app->when('App\Handlers\Commands\CreateOrderHandler')    ->needs('$maxOrderCount')    ->give(10);
Copy after login

标签

少数情况下我们需要解析特定分类下的所有绑定,比如,也许你正在构建一个接收多个不同 Report接口实现的报告聚合器,在注册完 Report实现之后,可以通过 tag方法给它们分配一个标签:

$this->app->bind('SpeedReport', function () {    //});$this->app->bind('MemoryReport', function () {    //});$this->app->tag(['SpeedReport', 'MemoryReport'], 'reports');
Copy after login

这些服务被打上标签后,可以通过 tagged方法来轻松解析它们:

$this->app->bind('ReportAggregator', function ($app) {    return new ReportAggregator($app->tagged('reports'));});
Copy after login

3、解析

有很多方式可以从容器中解析对象,首先,你可以使用 make方法,该方法接收你想要解析的类名或接口名作为参数:

$fooBar = $this->app->make('FooBar');
Copy after login

其次,你可以以数组方式访问容器,因为其实现了 PHP 的 ArrayAccess接口:

$fooBar = $this->app['FooBar'];
Copy after login

最后,也是最常用的,你可以简单的通过在类的构造函数中对依赖进行类型提示来从容器中解析对象,包括控制器、事件监听器、队列任务、中间件等都是通过这种方式。在实践中,这是大多数对象从容器中解析的方式。

容器会自动为其解析类注入依赖,比如,你可以在控制器的构造函数中为应用定义的仓库进行类型提示,该仓库会自动解析并注入该类:

<?phpnamespace App\Http\Controllers;use Illuminate\Routing\Controller;use App\Users\Repository as UserRepository;class UserController extends Controller{    /**     * 用户仓库实例     */    protected $users;    /**     * 创建一个控制器实例     *     * @param  UserRepository  $users     * @return void     */    public function __construct(UserRepository $users)    {        $this->users = $users;    }    /**     * 通过指定ID显示用户     *     * @param  int  $id     * @return Response     */    public function show($id)    {        //    }}
Copy after login

4、容器事件

服务容器在每一次解析对象时都会触发一个事件,可以使用 resolving方法监听该事件:

$this->app->resolving(function ($object, $app) {    // 容器解析所有类型对象时调用});$this->app->resolving(function (FooBar $fooBar, $app) {    // 容器解析“FooBar”对象时调用});
Copy after login

正如你所看到的,被解析的对象将会传递给回调,从而允许你在对象被传递给消费者之前为其设置额外属性。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles