managing class dependencies and performing dependency injection.
Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.
Almost all of your service container bindings will be registered within service providers, so most of these examples will demonstrate using the container in that context.
There is no need to bind classes into the container if they do not depend on any interfaces. The container does not need to be instructed on how to build these objects, since it can automatically resolve these objects using reflection.
Simple Bindings
Within a service provider, you always have access to the container via the $this->app property. We can register a binding using the bind method, passing the class or interface name that we wish to register along with a Closure that returns an instance of the class:
$this->app->bind('HelpSpot\API', function ($app) { return new HelpSpot\API($app->make('HttpClient')); });
Note that we receive the container itself as an argument to the resolver. We can then use the container to resolve sub-dependencies of the object we are building.
Binding A Singleton 单例
The singleton method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container:
$this->app->singleton('HelpSpot\API', function ($app) { return new HelpSpot\API($app->make('HttpClient')); });
Binding Instances
You may also bind an existing object instance into the container using the instance method. The given instance will always be returned on subsequent calls into the container:
$api = new HelpSpot\API(new HttpClient); $this->app->instance('HelpSpot\Api', $api);
Binding Primitives绑定常用的基本类型
Sometimes you may have a class that receives some injected classes, but also needs an injected primitive value such as an integer. You may easily use contextual binding to inject any value your class may need:
$this->app->when('App\Http\Controllers\UserController') ->needs('$variableName') ->give($value);
Binding Interfaces To Implementations
$this->app->bind( 'App\Contracts\EventPusher', 'App\Services\RedisEventPusher' );
Contextual Binding
解析:
You may use the make method to resolve a class instance out of the container. The make method accepts the name of the class or interface you wish to resolve:
$api = $this->app->make('HelpSpot\API');
The service container fires an event each time it resolves an object. You may listen to this event using the resolving method:
allowing you to set any additional properties on the object before it is given to its consumer.

Laravel之Service Container服务容器的更多相关文章

  1. laravel框架总结(四) -- 服务容器

    1.依赖 我们定义两个类:class Supperman 和 class Power,现在我们要使用Supperman ,而Supperman 依赖了Power class Supperman { p ...

  2. IOC Container(服务容器)的工作机制

    IOC Container 是laravel的一个核心内容,有了IOC Container在Laravel的强大表现,我们可以在Laravel中实现很大程度的代码维护性.(文档我是看的懵逼懵逼的(*^ ...

  3. Laravel源码解析 — 服务容器

    前言 本文对将系统的对 Laravel 框架知识点进行总结,如果错误的还望指出 阅读书籍 <Laravel框架关键技术解析> 陈昊 学习课程 Laravel5.4快速开发简书网站 轩脉刃 ...

  4. 简单理解laravel框架中的服务容器,服务提供者以及怎样调用服务

      laravel被称为最优雅的框架,最近正在学习中,对于用惯了thinkphp.ci框架的人来说,服务容器.服务提供者,依赖注入这些概念简直是一脸懵逼.我花了些时间梳理了一下,也不敢确定自己说的是对 ...

  5. Ioc容器与laravel服务容器初探

    一.Ioc容器 某天,小J心血来潮,决定建造一艘星舰,这艘星舰要搭载"与众不同最时尚,开火肯定棒"的电磁炮.于是他写了一个星舰类: class ElectromagneticGun ...

  6. laravel服务容器(IOC控制反转,DI依赖注入),服务提供者,门脸模式

    laravel的核心思想: 服务容器: 容器:就是装东西的,laravel就是一个个的对象 放入:叫绑定 拿出:解析 使用容器的目的:这里面讲到的是IOC控制反转,主要是靠第三方来处理具体依赖关系的解 ...

  7. laravel5.5的服务容器分析

    简介 服务容器是Laravel的核心.见名知意,服务容器就是一个存放服务的地方,当我们需要某个服务的时候,我们就可以从这个容器中取出我们需要的服务.用更专业一点的术语来说,官网定义服务容器是这样的: ...

  8. 怎么使用 Laravel 的服务容器来优化读写数据库中的 options关键词

    其中我们可以最方便地利用的一个特性就是 Laravel 的服务容器了.在这里我不多赘述 Service Container 是个啥,想了解的可以自行搜索.不想了解的就只要大致知道它是个可以 绑定/取出 ...

  9. laravel服务容器-----深入理解控制反转(IoC)和依赖注入(DI)

    首先大家想一想什么是容器,字面意思就是盛放东西的东西,常见的变量,对象属性都是容器,一个容器能够装什么东西,完全在于你对这个容器的定义.有的容器不仅仅只是存文本,变量,而是对象,属性,那么我们通过这种 ...

随机推荐

  1. Django~automated tests

    def xx(): 冒号下一行要缩进 ATD http://blog.csdn.net/doupei2006/article/details/7657547 http://www.jb51.net/a ...

  2. c/c++与Python的语法差异

    1.程序块语法方面: c/c++中用一对“{}”将多段语句括起来,表示一个程序块,并以右大括号表明程序块结束 ;i<n;i++) { cout<<a[i]; j+=; } Pytho ...

  3. MPAndroidChart 教程

    以前没用过MPAndroidChart,为了方便学习查找,引用下别个大神的笔记. 其余文章索引: MPAndroidChart 教程:概述MPAndroidChart 教程:开始 Getting St ...

  4. 【leetcode】Gray Code (middle)

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  5. 【EM】C++代码实现

    看了原理和比人的代码后,终于自己写了一个EM的实现. 我从网上找了一些身高性别的数据,用EM算法通过身高信息来识别性别. 实现的效果还行,正确率有84% (初始数据 男生170 女生160 方差都是1 ...

  6. ios企业应用发布流程

    转载自:http://blog.csdn.net/justinjing0612/article/details/8758692留作备忘 企业发布app的 过程比app store 发布的简单多了,没那 ...

  7. 解决【无法启动此程序,因为计算机中丢失MSVCP71.dll。尝试重新安装该程序以解决此问题】问题

    今日电脑偶然出现:[无法启动此程序,因为计算机中丢失MSVCP71.dll.尝试重新安装该程序以解决此问题.]的不断地弹窗报错. 在网上查找了好久,也没有找到解决办法,就在我将要准备重装系统的时候,又 ...

  8. 股票交易(洛谷U6084)

    题目背景 kkk最近迷上了炒股. 题目描述 kkk炒了N天股,第i天的股价为a[i]元.kkk希望股票每天都上涨1元钱,但是操盘手lzn并不想让kkk赚很多钱导致他亏本,于是a[i]相对a[i-1]就 ...

  9. java1.8中Lambda表达式reduce聚合测试例子

    public class LambdaTest { public static void main(String[] args) { // 相当于foreach遍历操作结果值 Integer out ...

  10. 20145206《Java程序设计》实验五Java网络编程及安全

    20145206<Java程序设计>实验五 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验步骤 我和201451 ...