Laravel之Service Container服务容器
2016-09-30 18:29
846 查看
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.BindingAlmost 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 BindingsWithin 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 InstancesYou 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 EventsThe 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
相关文章推荐
- laravel 服务容器service container和服务提供者service provider
- Laravel之Service Container服务容器
- symfony2 服务容器(Service Container)
- 如何使用Azure Container Service Engine在Azure中国区部署容器服务(一):DC/OS篇
- 【知识】Amazon EC2 Container Service (ECS) 容器服务到底是啥?(特点、原理、视频)
- Laravel 服务容器 Container 基本用法
- 222 laravel 服务容器,ServiceProvider,Facade的关系
- 如何使用Azure Container Service Engine在Azure中国区部署容器服务(一):DC/OS篇
- laravel 服务容器实例——深入理解IoC模式
- laravel服务容器-----深入理解控制反转(IoC)和依赖注入(DI)
- Laravel 服务容器实例教程 —— 深入理解控制反转(IoC)和依赖注入(DI)
- Laravel 学习笔记 —— 神奇的服务容器
- laravel5.2总结--服务容器(依赖注入,控制反转)
- docker容器中启动service服务 Failed to get D-Bus connection: Operation not permitted
- laravel容器container 阅读记录
- Laravel 学习笔记 —— 神奇的服务容器
- Laravel 服务容器实例教程 —— 深入理解控制反转(IoC)和依赖注入(DI)
- 9.Laravel5学习笔记:在laravel中注冊自己的服务到容器中
- 每天laravel[031]-laravel 基础知识 --- 服务容器
- laravel 学习笔记 —— 神奇的服务容器