繁体   English   中英

Symfony 5 通过别名注入服务

[英]Symfony 5 inject service by alias

我有一个服务叫:

<?php
class CustomService
{
    private $string;
    private $service1;
    private $service2;
    
    public function __construct($string, Service1 $service1, Service2 $service2)
    {
        $this->string = $string;
        $this->service1 = $service1;
        $this->service2 = $service2;
    }

    //Custom functions
}

$string 参数是一个可以改变其值的字符串。

我想从其他服务中调用此服务,但是有没有办法在我的 services.yaml 中使用某种别名定义“CustomService”?

service:
    App\Service\CustomService:
        alias: preService1
        arguments:
            $string: 'ABCD'
    
    App\Service\CustomService:
        alias: preService2
        arguments:
            $string: 'EFGH'

并在另一个服务中注入由别名定义的服务之一:

<?php
class PurchaseService
{
    public function __construct(CustomService $preService1)
    {
        //Here $preService1->string must be 'ABCD'
    }
    
    //Or in other functions in this service $preService1->string must be 'ABCD'
}

我有一个服务叫:

<?php
class CustomService
{
    private $string;
    private $service1;
    private $service2;
    
    public function __construct($string, Service1 $service1, Service2 $service2)
    {
        $this->string = $string;
        $this->service1 = $service1;
        $this->service2 = $service2;
    }

    //Custom functions
}

$string 参数是一个可以改变其值的字符串。

我想从其他服务调用此服务,但是有没有办法在我的 services.yaml 中使用某种别名定义“CustomService”?

service:
    App\Service\CustomService:
        alias: preService1
        arguments:
            $string: 'ABCD'
    
    App\Service\CustomService:
        alias: preService2
        arguments:
            $string: 'EFGH'

并将别名定义的服务之一注入另一个服务中:

<?php
class PurchaseService
{
    public function __construct(CustomService $preService1)
    {
        //Here $preService1->string must be 'ABCD'
    }
    
    //Or in other functions in this service $preService1->string must be 'ABCD'
}

我刚刚遇到了一个非常相似的问题,这就是我得到的解决方案。 您需要将 CustomService 的别名注入 PurchasService。

不必禁用自动连线,因此仍会自动连线附加参数。

service:
    app.service.pre_service1:
        class: App\Service\CustomService
        arguments:
            $string: 'ABCD'
            
    app.service.pre_service2:
        class: App\Service\CustomService
        arguments:
            $string: 'EFGH'
            
    app.service.purchase_service:
        class: App\Service\PurchaseService
        arguments:
            $preService1: '@app.service.pre_service1'
            
    App\Service\PurchaseService:
        alias: app.service.purchase_service

我有一个服务叫:

<?php
class CustomService
{
    private $string;
    private $service1;
    private $service2;
    
    public function __construct($string, Service1 $service1, Service2 $service2)
    {
        $this->string = $string;
        $this->service1 = $service1;
        $this->service2 = $service2;
    }

    //Custom functions
}

$string 参数是一个可以改变其值的字符串。

我想从其他服务调用此服务,但是有没有办法在我的 services.yaml 中使用某种别名定义“CustomService”?

service:
    App\Service\CustomService:
        alias: preService1
        arguments:
            $string: 'ABCD'
    
    App\Service\CustomService:
        alias: preService2
        arguments:
            $string: 'EFGH'

并将别名定义的服务之一注入另一个服务中:

<?php
class PurchaseService
{
    public function __construct(CustomService $preService1)
    {
        //Here $preService1->string must be 'ABCD'
    }
    
    //Or in other functions in this service $preService1->string must be 'ABCD'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM