我开始研究Laravel,但我不理解Service Container的概念。
它是如何工作的,开发人员需要知道什么才能在Laravel中充分利用这个概念?
发布于 2016-05-05 06:27:23
Laravel中的Service Container是应用程序的依赖注入容器和注册表
与手动创建对象相比,使用Service Container的优势在于:
能够管理对象创建上的类依赖关系
您可以在应用程序的一个点(绑定)定义如何创建对象,每次需要创建新实例时,只需向服务容器发出请求,服务容器将为您创建该实例以及所需的依赖项
例如,不使用new
关键字手动创建对象:
//every time we need YourClass we should pass the dependency manually
$instance = new YourClass($dependency);
您可以在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 );
});
并通过服务容器创建实例,具体如下:
//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
类,获得了容器的所有功能
发布于 2018-01-13 20:09:01
Laravel容器从服务(类)为完整的应用程序创建实例我们不需要为我们的应用程序创建instance
,比如
$myclass = new MyClass();
$mymethod = $myclass->myMethod();
应用程序::绑定
首先,我们将查看App
类的绑定静态方法。bind
只是将您的类instance
(对象)与应用程序绑定在一起,仅此而已。
App::bind('myapp', function(){
return new MyClass();
});
现在,我们可以通过使用App
类的静态方法make
将此对象用于我们的应用程序。
$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);
实现绑定
呀,我们有一个问题,我们如何在我们的应用中实现绑定?我们可以在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
);
}
}
结论:服务容器帮助创建类或服务的对象。
https://stackoverflow.com/questions/37038830
复制相似问题