Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel服务提供商解释_Php_Laravel_Symfony - Fatal编程技术网

Php Laravel服务提供商解释

Php Laravel服务提供商解释,php,laravel,symfony,Php,Laravel,Symfony,我对Laravel服务提供商不太熟悉,对此我有一个问题 示例:我有三个类SystemProfiler、SurveyProfiler和OfferProfiler,它们实现了ProfilerInterface。我还有一个ProfilerService类,它在构造函数中注入ProfilerInterface。我需要通过注入每个分析器来创建不同的ProfilerService服务 档案服务: class ProfilerService { $this->profiler; fu

我对Laravel服务提供商不太熟悉,对此我有一个问题

示例:我有三个类SystemProfiler、SurveyProfiler和OfferProfiler,它们实现了ProfilerInterface。我还有一个ProfilerService类,它在构造函数中注入ProfilerInterface。我需要通过注入每个分析器来创建不同的ProfilerService服务

档案服务:

class ProfilerService {

    $this->profiler;

    function __construct(ProfilerInterface $profiler) {
            $this->profiler = profiler;
    }
}
system_profiler:
    class: App\MyBundle\Profiles\SystemProfiler

survey_profiler:
    class: App\MyBundle\Profiles\SurveyProfiler

offer_profiler:
    class: App\MyBundle\Profiles\OfferProfiler

system_profile_service:
    class: App\MyBundle\Services\ProfilerService
    arguments:
        - system_profiler

survey_profile_service:
    class: App\MyBundle\Services\ProfilerService
    arguments:
        - survey_profiler

offer_profile_service:
    class: App\MyBundle\Services\ProfilerService
    arguments:
        - offer_profiler
我知道如何在symfony2框架中做到这一点:

class ProfilerService {

    $this->profiler;

    function __construct(ProfilerInterface $profiler) {
            $this->profiler = profiler;
    }
}
system_profiler:
    class: App\MyBundle\Profiles\SystemProfiler

survey_profiler:
    class: App\MyBundle\Profiles\SurveyProfiler

offer_profiler:
    class: App\MyBundle\Profiles\OfferProfiler

system_profile_service:
    class: App\MyBundle\Services\ProfilerService
    arguments:
        - system_profiler

survey_profile_service:
    class: App\MyBundle\Services\ProfilerService
    arguments:
        - survey_profiler

offer_profile_service:
    class: App\MyBundle\Services\ProfilerService
    arguments:
        - offer_profiler
然后用ProfilerService实现的别名调用
$this->container->get()

但是Laravel文档说,“如果类不依赖于任何接口,就没有必要将它们绑定到容器中。”。并且ProfilerService不依赖于接口。因此,我可以将每个探查器绑定到接口,如下所示:

   $this->app->bind('App\MyBundle\Contracts\ProfilerInterface','App\MyBundle\Profiles\SystemProfiler');

但是我应该如何绑定哪些探查器应该注入到ProfilerService以及何时???


如果您能提供任何帮助和解释,我将不胜感激。您的
ProfilerService
类型的构造函数提示了一个接口,这意味着您的
ProfilerService
确实依赖于一个接口

如果您试图
App::make('App\MyBundle\Services\ProfilerService'),则无需任何其他设置,则会出现错误,因为Laravel不知道如何解决接口依赖关系

然后执行
$this->app->bind('app\MyBundle\Contracts\ProfilerInterface','app\MyBundle\Profiles\SystemProfiler')
在您的服务提供商中,您告诉Laravel“每当您需要解析ProfilerInterface时,请创建一个新的SystemProfiler”

使用该绑定设置,如果您随后尝试
App::make('App\MyBundle\Services\ProfilerService'),Laravel将创建一个新的
ProfilerService
实例,并在构造函数中注入一个新的
SystemProfiler
实例

但是,这并不是您想要的,因为您有三种不同的
ProfilerInterface
实现。你不希望拉威尔总是只注射一次。在本例中,您将创建自定义绑定,类似于您在Symfony中所做的

在您提供的服务中,您的绑定如下所示:

$this->app->bind('system_profile_service', function($app) {
    return $app->make('App\MyBundle\Services\ProfilerService', [$app->make('App\MyBundle\Profiles\SystemProfiler')]);
});

$this->app->bind('survey_profile_service', function($app) {
    return $app->make('App\MyBundle\Services\ProfilerService', [$app->make('App\MyBundle\Profiles\SurveyProfiler')]);
});

$this->app->bind('offer_profile_service', function($app) {
    return $app->make('App\MyBundle\Services\ProfilerService', [$app->make('App\MyBundle\Profiles\OfferProfiler')]);
});
现在,通过这些绑定设置,您可以在需要时从IOC解析自定义绑定

$systemProfiler = App::make('system_profiler_service');
$surveyProfiler = App::make('survey_profile_service');
$offerProfiler = App::make('offer_profile_service');
这里是():

用法: 鉴于此设置:

namespace App;

class ImplOne implements SomeInterface {}

class ImplTwo implements SomeInterface {}

class DependantOne
{
    public function __construct(SomeInterface $dependency)
    {
        $this->dependency = $dependency;
    }
}

class DependantTwo
{
    public function __construct(SomeInterface $dependency)
    {
        $this->dependency = $dependency;
    }
}

据我所知,我可以使用
$this->app->bind('system\u profile\u service',function($app){returnnewprofilerservice($app->make(SystemProfiler::class),;})但我仍然不确定这是否是一种正确的解释方式,我想我错过了文档中的一些内容(使用源代码Luke;)
$ php artisan tinker

// Aliases
>>> app('some_service.one')
=> App\ImplOne {#669}
>>> app('some_service.two')
=> App\ImplTwo {#671}

// Aliased interface
>>> app('App\SomeInterface')
=> App\ImplOne {#677}
>>> app('App\DependantOne')->dependency
=> App\ImplOne {#677}

// Contextual
>>> app('App\DependantTwo')->dependency
=> App\ImplOne {#676}
namespace App;

class ImplOne implements SomeInterface {}

class ImplTwo implements SomeInterface {}

class DependantOne
{
    public function __construct(SomeInterface $dependency)
    {
        $this->dependency = $dependency;
    }
}

class DependantTwo
{
    public function __construct(SomeInterface $dependency)
    {
        $this->dependency = $dependency;
    }
}