Laravel中Service Container的概念是什么? [英] What is the concept of Service Container in Laravel?

查看:27
本文介绍了Laravel中Service Container的概念是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始研究 Laravel,但我不了解服务容器的概念.

I am starting to look into Laravel but I don't understand the concept of Service Container.

它是如何工作的,开发人员需要知道什么才能在 Laravel 中充分利用这个概念?

How does it work and what do developers need to know to fully utilize this concept in Laravel?

推荐答案

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

The Service Container in Laravel is a Dependency Injection Container and a Registry for the application

与手动创建对象相比,使用服务容器的优点是:

The advantages of using a Service Container over creating manually your objects are:

能够管理对象创建的类依赖

你定义了一个对象应该如何在应用程序的一个点(绑定)创建,每次你需要创建一个新实例时,你只需向服务容器询问它,它就会为你创建它,一起具有所需的依赖项

You define how a object should be created in one point of the application (the binding) and every time you need to create a new instance, you just ask it to the service container, and it will create it for you, along with the required dependencies

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

For example, instead of creating objects manually with the new keyword:

//every time we need YourClass we should pass the dependency manually
$instance = new YourClass($dependency);

您可以在服务容器上注册一个绑定:

you can register a binding on the Service Container:

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

并通过服务容器创建一个实例:

and create an instance through the service container with:

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

接口绑定到具体类

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

With Laravel automatic dependency injection, when an interface is required in some part of the app (i.e. in a controller's constructor), a concrete class is instantiated automatically by the Service Container. Changing the concrete class on the binding, will change the concrete objects instantiated through all your app:

//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 方法进行绑定,从而将容器用作注册表.

You can create and store unique object instances on the container and get them back later: using the App::instance method to make the binding, and thus using the container as a Registry.

// 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类,获得了容器的所有功能

As a final note, essentially the Service Container -is- the Application object: it extends the Container class, getting all the container's funtionalities

这篇关于Laravel中Service Container的概念是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆