Laravel中服务容器的概念是什么?

时间:2016-05-04 22:01:02

标签: php laravel design-patterns laravel-5 laravel-5.2

我开始研究Laravel,但我不理解Service Container的概念。

它如何运作以及开发人员需要了解什么才能在Laravel中充分利用这一概念?

2 个答案:

答案 0 :(得分:40)

Laravel中的Service Container是一个依赖注入容器和应用程序的注册表

使用服务容器而不是手动创建对象的优点是:

管理对象创建的类依赖性的能力

您可以定义如何在应用程序的一个点(绑定)中创建对象,并且每次需要创建新实例时,只需将其提交给服务容器,它就会为您创建它,具有所需的依赖性

例如,不要使用NULL关键字手动创建对象:

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 );
});

将接口绑定到具体类

使用Laravel自动依赖注入时,当应用程序的某些部分需要一个接口时(即在控制器的构造函数中),服务容器会自动实例化一个具体的类。更改绑定上的具体类将更改通过所有应用程序实例化的具体对象:

//no need to create the YourClass dependencies, the SC will do that for us!
$instance = App::make( YourClass::class );

将服务容器用作注册表

您可以在容器上创建和存储唯一对象实例,并在以后将其取回:使用//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类,获得了所有容器的功能

答案 1 :(得分:4)

Laravel容器从服务(类)创建完整应用程序的实例 我们不需要为我们的应用程序创建instance,例如

$myclass = new MyClass();
$mymethod = $myclass->myMethod();
  

应用::结合

首先,我们将看看App类的绑定静态方法。 bind只是将您的类instance(对象)与应用程序绑定,仅此而已。

App::bind('myapp', function(){
    return new MyClass();
});

现在,我们可以使用make App类的静态方法将此对象用于我们的应用程序。

$myclass = App::make(MyClass::class);
$mymethod = $myclass->myMethod();
  

应用::单

在上面的示例中,当我们打算调用make方法时,它会在每次新的instance类时生成,因此So Laravel为Singleton提供了非常好的解决方案 我们可以通过object方法将singleton绑定到我们的应用程序。

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
      );
  }
}
  

结论:服务容器有助于创建类或对象   服务。

相关问题