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. c语言实现面向对象OOC

    这种问题比较锻炼思维,同时考察c和c++的掌握程度.如果你遇到过类似问题,此题意义自不必说.如果用c实现c++,主要解决如何实现封装,继承和多态三大问题,本文分两块说. 1.封装 // Example ...

  2. nyoj133_子序列_离散化_尺取法

    子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...

  3. codeforces 557B. Pasha and Tea 解题报告

    题目链接:http://codeforces.com/problemset/problem/557/B 题目意思:有 2n 个茶杯,规定第 i 个茶杯最多只能装 ai 毫升的水.现在给出 w 毫升的水 ...

  4. JAVA导出pdf实例

    一.直接导出成PDF   Java代码 1. import java.io.FileNotFoundException; 2. import java.io.FileOutputStream; 3.  ...

  5. mvc EF

    一:数据库不存在时重新创建数据库 复制内容到剪贴板程序代码 Database.SetInitializer<testContext>(new CreateDatabaseIfNotExis ...

  6. 【hadoop2.6.0】通过代码运行程序流程

    之前跑了一下hadoop里面自带的例子,现在顺一下如何通过源代码来运行程序. 我懒得装eclipse,就全部用命令行了. 整体参考官网上的:http://hadoop.apache.org/docs/ ...

  7. 【python】类中的super().__init__()

    来源:百度知道 python中的super( test, self).__init__()是什么意思? 首先找到test的父类(比如是类A),然后把类test的对象self转换为类A的对象,然后&qu ...

  8. Sql order by 和 group BY一起使用时需要注意

    ORDER BY 子句中的列必须包含在聚合函数或 GROUP BY 子句中. slect * from table group by class,id order by id slect * from ...

  9. Hibernate单向一对多对象关系模型映射

    1 hibernate 的对象关系映射 Orm: 类-----表 属性------字段 对象------记录 表:在数据库中存在主外键的关系,反向工厂类是由表生成,在由表生成类的时候,类和类之间存在者 ...

  10. chaper3_exerise_UVa455_周期串

    #include<iostream> #include<cstring> #include<stdio.h> using namespace std; ; int ...