Nel*_*llo 44 php design-patterns laravel laravel-5 laravel-5.2
我开始研究Laravel,但我不理解Service Container的概念.
它是如何工作的以及开发人员需要知道什么才能在Laravel中充分利用这一概念?
Mop*_*ppo 54
Laravel中的服务容器是依赖注入容器和应用程序的注册表
使用服务容器而不是手动创建对象的优点是:
管理对象创建的类依赖性的能力
您可以定义如何在应用程序的一个点(绑定)中创建对象,并且每次需要创建新实例时,只需将其提交给服务容器,它将为您创建它以及所需的依赖
例如,不是使用new关键字手动创建对象:
//every time we need YourClass we should pass the dependency manually
$instance = new YourClass($dependency);
Run Code Online (Sandbox Code Playgroud)
您可以在服务容器上注册绑定:
//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 );
});
Run Code Online (Sandbox Code Playgroud)
并通过服务容器创建一个实例:
//no need to create the YourClass dependencies, the SC will do that for us!
$instance = App::make( YourClass::class );
Run Code Online (Sandbox Code Playgroud)
将接口绑定到具体类
使用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 );
Run Code Online (Sandbox Code Playgroud)
使用服务容器作为注册表
您可以在容器上创建和存储唯一对象实例,并在以后将其取回:使用该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');
Run Code Online (Sandbox Code Playgroud)
作为最后一点,基本上是服务容器 - 是Application对象:它扩展了Container类,获得了所有容器的功能
小智 7
Laravel容器从services(class)为整个应用程序创建实例我们不需要instance为我们的应用程序创建像
$myclass = new MyClass();
$mymethod = $myclass->myMethod();
Run Code Online (Sandbox Code Playgroud)
App :: bind
首先,我们来看一下绑定App类的静态方法。bind只是将您的类instance(对象)与应用程序绑定,仅此而已。
App::bind('myapp', function(){
return new MyClass();
});
Run Code Online (Sandbox Code Playgroud)
现在,我们可以通过使用类make的静态方法将该对象用于我们的应用程序App。
$myclass = App::make(MyClass::class);
$mymethod = $myclass->myMethod();
Run Code Online (Sandbox Code Playgroud)
应用程式:: singleton
在上面的示例中,当我们要调用make方法时instance,每当类新的时候它就会生成,因此Laravel有一个不错的解决方案,因为Singleton
我们可以object通过singleton方法将an绑定到我们的应用程序。
App::singleton(MyClass::class, function(){
return new MyClass();
});
Run Code Online (Sandbox Code Playgroud)
我们可以通过make方法解决。现在,我们始终从该方法收到完全相同的实例。
$myclass = App::make(MyClass::class);
$mymethod = $myclass->myMethod();
Run Code Online (Sandbox Code Playgroud)
App :: instance我们可以将实例绑定到容器,并且我们将始终使用instance方法返回完全相同的实例。
$myclass = new MyClass();
App::instance(MyClass::class, $myclass);
Run Code Online (Sandbox Code Playgroud)
我们可以通过解决
$myclass = App::make(MyClass::class);
Run Code Online (Sandbox Code Playgroud)
我们可以通过绑定接口
App::instance(MyClassInterface::class, new MyClass);
Run Code Online (Sandbox Code Playgroud)
实现绑定
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
);
}
}
Run Code Online (Sandbox Code Playgroud)
结论:服务容器有助于创建类或服务的对象。