簡體   English   中英

Symfony3.4:如何使用參數指定服務

[英]Symfony3.4: How to specify a service with an argument

在 Symfony3.4 中,支持自動布線時出現以下錯誤。
如果使用$articleType指定服務名稱和顯示相應文章的現有代碼,則會出現以下錯誤。
將多個服務傳遞給 __construct 不起作用。
有什么好辦法嗎?

https://symfony.com/doc/3.4/service_container/3.3-di-changes.html
Controller.php

    private function getArticleSummary($articleType)
    {
        $summary = array();
        $articleService = $this->get('admin.'.$articleType.'Service');
        foreach (array('draft', 'pending', 'reject', 'publish', 'hidden') as $articleStatus) {
            $params = array(
                'articleType' => $articleType,
                'articleStatus' => $articleStatus,
            );
            $summary[$articleStatus] = $articleService->countArticleBySearchParams($params);
        }
        return $summary;
    }

服務.yml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    App\:
        resource: '../../src/*'
        exclude: '../../src/{Entity,Repository, Ahi/Sp/AdminBundle/Resources/public/uploadify, Ahi/Sp/AdminBundle/Ahi/Sp/PublicBundle/ }'

    App\Ahi\Sp\AdminBundle\Controller\:
        resource: '../../src/Ahi/Sp/AdminBundle/Controller'
        public: true
        tags: ['controller.service_arguments']

    admin.brandeventService:
      autowire: true
      autoconfigure: true
      class: 'App\Ahi\Sp\AdminBundle\Model\Service\BrandEventService'

    App\Ahi\Sp\AdminBundle\Controller\Hq\DefaultController:
      arguments: [ '@admin.brandeventService' ]

    App\Ahi\Sp\AdminBundle\Model\Service\BrandEventService: '@admin.brandeventService'

當前代碼中的錯誤

Service "admin.brandeventService" not found:  
even though it exists in the app's container,  
the container inside "App\Ahi\Sp\AdminBundle\Controller\Hq\DefaultController"  
is a smaller service locator that only knows about the "doctrine"

試過的代碼

  • 將 $brandEventService 添加到 articleService 的 __construct
  • 將 __construct 和 $brandEventService 添加到 DefaultController
  • 在 services.yml 中的 DefaultController 中設置服務
  • 添加 BrandEventService $brandEventService 到 getArticleSummary

DefaultController.php

    protected $brandEventService;

    public function __construct(BrandEventService $brandEventService)
    {
        $this->brandEventService = $brandEventService;
    }

php bin/console debug:container admin.brandeventService的嘗試結果

Information for Service "admin.brandeventService"
=================================================

 ---------------- -------------------------------------------------------- 
  Option           Value                                                   
 ---------------- -------------------------------------------------------- 
  Service ID       admin.brandeventService                                 
  Class            App\Ahi\Sp\AdminBundle\Model\Service\BrandEventService  
  Tags             -                                                       
  Public           no                                                      
  Synthetic        no                                                      
  Lazy             no                                                      
  Shared           yes                                                     
  Abstract         no                                                      
  Autowired        yes                                                     
  Autoconfigured   yes                                                     
 ---------------- -------------------------------------------------------- 

您應該像這樣修改您的services.yaml

services:
  _defaults:
    autowire: true
    autoconfigure: true

  admin.shopeventService:
    autowire: true
    autoconfigure: true
    public: true
    class: App\Admin\Media\Provider\MultiUploadFileProvider

    # ...

    App\Ahi\Sp\AdminBundle\Model\Service\ShopEventService: '@admin.shopeventService'


    # same here
    # ...
   App\Ahi\Sp\AdminBundle\Model\Service\BrandEventService: '@admin.brandeventService'

有關更多詳細信息,您可以查看此鏈接https://symfony.com/doc/current/service_container/autowiring.html#using-aliases-to-enable-autowiring

希望這會有所幫助。

答案基於問題的上下文。 這里的目標是將舊版 Symfony 2 應用程序最終更新到 4.4。 試圖在不重寫太多應用程序的情況下讓代碼在 3.4 下運行。

基本問題是,當使用 Symfony 的 AbstractController class 時,只能使用 $this->get() 訪問特定的服務。 您可以查看源代碼以了解發生了什么,但本質上 ServiceSubscriberInterface::getSubscribedServices() 方法用於指定哪些服務可用。 因此,解決此問題的最快方法是將文章類型服務添加到派生的 controller 中:

class DefaultController extends AbstractController
{
    public static function getSubscribedServices() : array
    {
        $services = parent::getSubscribedServices();

        $services['admin.brand'] = BrandEventService::class;
        $services['admin.other'] = OtherEventService::class;

        return $services;
    }
    public function type(string $type = null) : Response
    {
        $serviceId = 'admin.' . $type;
        $service = $this->get($serviceId);
        return new Response('Article Type ' . $type);
    }

上面的代碼顯示了將兩個服務添加到控制器的服務定位器,然后在操作方法中拉動其中一個。 services.yaml 文件只是開箱即用的基本配置。 不需要別名或調整。 這應該足以讓你繼續前進。

一旦您了解這里發生了什么,您就可以返回 go 並閱讀有關服務定位器的部分,並可能了解處理此問題的“正確”方法。 但這實際上可能要等到 4.4,因為 Symfony 4 中引入了一些改進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM