Laravel laravel的服务提供者有什么用 [英] Laravel What is the use of service providers for laravel
问题描述
我只是Laravel的开始,我真的对service contains
和service providers
感到困惑,我在搜索一些示例,例如follow的服务代码:
l am just getting start of Laravel, and l really confused about service contains
and service providers
l searched for some examples like follow's service code:
namespace App\Service;
class Tests
{
public function test()
{
echo "aaa";
}
}
服务提供商的代码
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class TestServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register services.
*
* @return void
*/
public function register()
{
//
$this->app->bind('App\Service\Tests', function($app){
return new \App\Service\Tests();
});
}
}
然后我将此提供程序添加到config/app,php
-> providers
然后我创建一个控制器
Then l added this provider into config/app,php
-> providers
Then l create a controller
namespace App\Http\Controllers\test;
use App\Http\Controllers\Controller;
use App\Service\Tests as tests;
class Test extends Controller
{
public function index()
{
$t = new tests();
$t -> test();
}
}
所以,我可以像这样使用我的Tests
,为什么我需要通过依赖注入来使用它,例如官方网站,如:
So, l can use my Tests
like this, why l need to use it by Dependency injection like official site like:
public function index(tests $test)
{
$test->test();
}
我看了一些有关DI和IoC的文档或文章,但是,我只是不明白它的用途和好处
l saw some document or article about DI and IoC, but, but l just couldn't understand what is the use and the benefit about it
推荐答案
首先,Laravel使用服务容器和服务提供者,而不是服务器容器或服务器提供商:)
First of all, Laravel uses service container and service providers, not server container or server provider :)
使用依赖项注入(DI)有一些好处:
Here are some benefits of using dependencies injection (DI):
因为您的Test
类构造函数非常简单,所以您看不到依赖注入的好处.考虑一下这样的课程:
Because your Test
class constructor is quite simple, you don't see the benefit of dependencies injection. Think about a class like this:
class Complex {
public function __construct(
FooService $fooService,
BarService $barService,
int $configValue
) {
}
}
没有DI,您必须获取(或创建)$fooService
和$barService
的实例,每次每次从配置文件中检索$configValue
的值,而您想要一个新的实例. Complex
类.
Without DI, you have to get (or create) instances of $fooService
and $barService
, retrieve the value of $configValue
from the configuration files every time you want a new instance of the Complex
class.
使用DI,您可以告诉服务容器如何一次创建Complex
实例,然后容器可以通过一次调用(例如$container->make(Complex::class)
)为您提供正确的实例
With DI, you tell the service container how to create the Complex
instance once, then the container can give the correct instance for you with one call (e.g. $container->make(Complex::class)
)
继续前面的示例.如果FooService
和BarService
也依赖于其他类怎么办?
Continue with the previous example. What happens if the FooService
and BarService
depends on other classes, too?
没有DI,您必须创建依赖对象的实例(并希望它们不依赖于其他类).这通常以创建一个类的多个实例结束,浪费了代码和计算机内存.
Without DI, you have to create instances of the dependent objects (and hope that they do not depends on other classes). This usually ends with multiple instances of one class created, a waste of codes and computer memory.
使用DI,所有相关对象都由容器创建(您必须在容器之前注册这些类).容器还管理器,如果需要,每个类仅保留一个实例,这样可以节省代码量以及程序使用的内存量.
With DI, all dependent objects are created by the container (you have to register those classes with the container before). The container also manager to keep only one instance of each class if you want, which save the amount of code as well as the amount of memory used by your program.
要在当前请求的整个生命周期中仅保留该类的一个实例,可以使用singleton
方法而不是bind
To keep only one instance of the class in the whole life of the current request, you can register your class creation process with singleton
method instead of bind
这篇关于Laravel laravel的服务提供者有什么用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!