Laravel之Service Container服务容器
来源:互联网 发布:优质rs485数据电缆价格 编辑:程序博客网 时间:2024/05/16 05:28
managing class dependencies and performing dependency injection.
Dependency injection is a fancy phrase that essentiallymeans this: class dependencies are "injected" into the classvia the constructor or, in some cases, "setter" methods.
Binding
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 containerif theydo 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 haveaccess 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){returnnew 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){returnnew 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');
Container Events
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.
https://laravel.com/docs/5.3/container
0 0
- Laravel之Service Container服务容器
- laravel 服务容器service container和服务提供者service provider
- symfony2 服务容器(Service Container)
- laravel的 容器container(ioc)
- laravel服务容器
- Laravel 服务容器解析
- 【知识】Amazon EC2 Container Service (ECS) 容器服务到底是啥?(特点、原理、视频)
- Laravel 中的服务容器、服务、服务提供者
- laravel服务容器学习笔记
- Laravel中Service Provider另一种绑定到服务容器的方式和场景
- Laravel 之Service Providers
- 如何使用Azure Container Service Engine在Azure中国区部署容器服务(一):DC/OS篇
- 如何使用Azure Container Service Engine在Azure中国区部署容器服务(一):DC/OS篇
- 如何使用Azure Container Service Engine在Azure中国区部署容器服务(二):Kubernetes篇
- laravel核心之ioc容器
- 如何理解Laravel 的IOC 服务容器
- IOC的服务容器,laravel性质详解
- [李景山php]每天laravel[031]-laravel 基础知识 --- 服务容器
- struts2标签之<s:fielderror>
- JS实现网页全屏和退出
- druid连接池
- Fresco的使用
- slickedit 的安装方法
- Laravel之Service Container服务容器
- android实现app通过jni调用C/C++方法
- HandlerThread详解
- "><img src=hi onerror=alert(1)>
- Picasso的使用
- 基于排序的方法求一组数的中位数(冒泡排序&选择排序) -- 浙大《数据结构》第二版 例2.6
- Orcl表分区和表空间的简单介绍,及其简单的创建方法
- springMVC的显示登陆首页和错误时跳转错误界面
- LeetCode #233: Number of Digit One