绑定基础
几乎所有的服务容器绑定都是在 服务提供者 中完成。
在目录结构如下图

注:如果一个类没有基于任何接口那么就没有必要将其绑定到容器。容器并不需要被告知如何构建对象,因为它会使用 PHP 的反射服务自动解析出具体的对象。
简单的绑定
在一个服务提供者中,可以通过 $this->app 变量访问容器,然后使用 bind 方法注册一个绑定,该方法需要两个参数,第一个参数是我们想要注册的类名或接口名称,第二个参数是返回类的实例的闭包:
$this->app->bind('HelpSpotAPI', function ($app) {
return new HelpSpotAPI($app->make('HttpClient'));
});
注意到我们将容器本身作为解析器的一个参数,然后我们可以使用该容器来解析我们正在构建的对象的子依赖。
绑定一个单例
singleton 方法绑定一个只会解析一次的类或接口到容器,然后接下来对容器的调用将会返回同一个对象实例:
$this->app->singleton('HelpSpotAPI', function ($app) {
return new HelpSpotAPI($app->make('HttpClient'));
});
绑定原始值
你可能有一个接收注入类的类,同时需要注入一个原生的数值比如整型,可以结合上下文轻松注入这个类需要的任何值:
$this->app->when('AppHttpControllersUserController')
->needs('$variableName')
->give($value);
绑定接口到实现
服务容器的一个非常强大的功能是其绑定接口到实现。我们假设有一个 EventPusher 接口及其实现类 RedisEventPusher ,编写完该接口的 RedisEventPusher 实现后,就可以将其注册到服务容器:
$this->app->bind( 'AppContractsEventPusher', 'AppServicesRedisEventPusher' );
这段代码告诉容器当一个类需要 EventPusher 的实现时将会注入 RedisEventPusher,现在我们可以在构造器或者任何其它通过服务容器注入依赖的地方进行 EventPusher 接口的依赖注入:
use AppContractsEventPusher;
/**
* 创建一个新的类实例
*
* @param EventPusher $pusher
* @return void
*/
public function __construct(EventPusher $pusher){
$this->pusher = $pusher;
}
上下文绑定
有时侯我们可能有两个类使用同一个接口,但我们希望在每个类中注入不同实现,例如,两个控制器依赖 IlluminateContractsFilesystemFilesystem 契约的不同实现。Laravel 为此定义了简单、平滑的接口:
use IlluminateSupportFacadesStorage;
use AppHttpControllersVideoController;
use AppHttpControllersPhotoControllers;
use IlluminateContractsFilesystemFilesystem;
$this->app->when(PhotoController::class)
->needs(Filesystem::class)
->give(function () {
return Storage::disk('local');
});
$this->app->when(VideoController::class)
->needs(Filesystem::class)
->give(function () {
return Storage::disk('s3');
});







