【发布时间】:2016-08-30 13:07:21
【问题描述】:
我开始研究 Laravel 但我不了解服务容器的概念。
它是如何工作的,开发人员需要了解什么才能在 Laravel 中充分利用这一概念?
【问题讨论】:
标签: php laravel design-patterns laravel-5 laravel-5.2
我开始研究 Laravel 但我不了解服务容器的概念。
它是如何工作的,开发人员需要了解什么才能在 Laravel 中充分利用这一概念?
【问题讨论】:
标签: php laravel design-patterns laravel-5 laravel-5.2
Laravel 中的Service Container 是一个依赖注入容器和应用程序的注册表
与手动创建对象相比,使用服务容器的优点是:
能够管理对象创建的类依赖关系
您可以在应用程序的一个点(绑定)中定义一个对象应该如何创建,并且每次您需要创建一个新实例时,您只需向服务容器请求它,它就会为您创建它,同时具有所需的依赖项
例如,不用new 关键字手动创建对象:
//every time we need YourClass we should pass the dependency manually
$instance = new YourClass($dependency);
您可以在服务容器上注册一个绑定:
//add a binding for the class YourClass
App::bind( YourClass::class, function()
{
//do some preliminary work: create the needed dependencies
$dependency = new DepClass( config('some.value') );
//create and return the object with his dependencies
return new YourClass( $dependency );
});
并通过服务容器创建一个实例:
//no need to create the YourClass dependencies, the SC will do that for us!
$instance = App::make( YourClass::class );
将接口绑定到具体类
使用 Laravel 自动依赖注入,当应用程序的某些部分(即在控制器的构造函数中)需要接口时,服务容器会自动实例化一个具体类。更改绑定上的具体类,将更改通过您的所有应用实例化的具体对象:
//everityme a UserRepositoryInterface is requested, create an EloquentUserRepository
App::bind( UserRepositoryInterface::class, EloquentUserRepository::class );
//from now on, create a TestUserRepository
App::bind( UserRepositoryInterface::class, TestUserRepository::class );
将服务容器用作注册表
您可以在容器上创建和存储唯一的对象实例并稍后将它们取回:使用App::instance 方法进行绑定,从而将容器用作注册表。
// Create an instance.
$kevin = new User('Kevin');
// Bind it to the service container.
App::instance('the-user', $kevin);
// ...somewhere and/or in another class...
// Get back the instance
$kevin = App::make('the-user');
最后一点,服务容器本质上是Application 对象:它扩展了Container 类,获得了容器的所有功能
【讨论】:
YourClass 取决于 DepClass 和 DepClass 需要一个参数,即 config('some.value') 被初始化。假设我们需要一个数字作为用户的参数,而不是config('some.value')。那么App::bind和App::make会有什么变化呢?
Laravel 容器从服务(类)为完整的应用程序创建实例
我们不需要为我们的应用程序创建instance,例如
$myclass = new MyClass();
$mymethod = $myclass->myMethod();
应用::绑定
首先,我们来看App类的绑定静态方法。 bind 只是将您的类 instance(object) 与应用程序绑定,仅此而已。
App::bind('myapp', function(){
return new MyClass();
});
现在,我们可以通过使用make 类的静态方法App 将这个对象用于我们的应用程序。
$myclass = App::make(MyClass::class);
$mymethod = $myclass->myMethod();
应用::单例
在上面的示例中,当我们要调用 make 方法时,它每次都会生成新的 instance 类,所以 Laravel 有很好的 Singleton 解决方案
我们可以通过singleton 方法将object 绑定到我们的应用程序。
App::singleton(MyClass::class, function(){
return new MyClass();
});
我们可以通过make方法解决。现在,我们总是从这个方法中收到完全相同的实例。
$myclass = App::make(MyClass::class);
$mymethod = $myclass->myMethod();
应用::实例
我们可以将实例绑定到容器,并且我们将始终使用instance 方法返回完全相同的实例。
$myclass = new MyClass();
App::instance(MyClass::class, $myclass);
我们可以解决
$myclass = App::make(MyClass::class);
我们可以通过以下方式绑定接口
App::instance(MyClassInterface::class, new MyClass);
实现绑定
Yaa,我们有一个问题,我们如何在我们的应用程序中实现绑定?我们可以在AppServiceProvider中实现绑定
app/Providers/AppServiceProvider.php
namespace App\Providers;
use App\SocialProvider;
use App\TwitterSocialProvider;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(
MyClassInterface::class,
MyClass::class
);
}
}
结论:服务容器有助于创建类对象或 服务。
【讨论】: