Laravel之Service Container服务容器
转载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.
时间宝贵,只能复制+粘贴,若图片无法显示或排版混乱,请访问elesos.com查找原文
上一篇:Laravel错误与日志处理
下一篇:Linux环境变量设置指南

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Laravel:服务容器
部分,在它的调度下,框架各个组件可以很好的组合在一起工作。实际上,Laravel 的Application...
laravel 服务容器 服务提供者 生命周期 -
docker 之容器Container深入
文章目录Image到ContainerContainer到Imagecentos样例1.我们首先拉取最新版
docker容器Container深入 centos vim -
【Laravel系列6.2】Laravel中的服务容器
Laravel中的服务容器我们已经了解了服务容器是个什么东西,也知道了依赖、依赖注入
java spring python php 编程语言 -
Laravel服务容器(继承与事件)
前面写了容器的上下文绑定,接下去看下 继承与事件在Laravel 框架中,有时需要修改 系统的基
laravel 容器 php App git -
【Laravel系列6.1】服务容器简介
服务容器简介服务容器是 Laravel 非常核心的内容,也可以说是 Laravel 中最引人注目的地方。提到服一个类 B 的变化,我们就说这...
设计模式 java 编程语言 python php -
laravel容器
容器,字面上理解就是装东西的东西。常见的变量、对象属性等都可以算是容器。一个容器能够装什
初始化 依赖注入 工厂模式