【问题标题】:How can I display the name of all services with specific tag in symfony2 form如何以 symfony2 形式显示具有特定标签的所有服务的名称
【发布时间】:2013-05-16 23:19:14
【问题描述】:

我有几个使用相同标签标记的服务。我想知道如何继续检索所有这些服务的列表并将其注入表单?

这是我最后想要的示例:

$builder->add('days', 'choice', array(
    'choices' => $SERVICES,
    'multiple' => false,
    'expanded' => false,
    ))

【问题讨论】:

    标签: forms symfony service formbuilder


    【解决方案1】:

    主要思想是定义一个服务来处理特定类型的所有标记服务,然后在编译器传递中将标记服务添加到该服务。

    Working with Tagged Services 上的文档为例,您可以添加一个返回所有服务的方法:

    <?php
    
    // ...
    
    class TransportChain
    {
        // ...
    
        /**
         * @return array
         */
        public function getTransports()
        {
            return $this->transports;
        }
    }
    

    然后从您可以访问 DIC 的任何地方,只需使用:

    // Get access to all services tagged with "acme_mailer.transport"
    $transports = $this
        ->getContainer()
        ->get('acme_mailer.transport_chain')
        ->getTransports()
    ;
    

    当然,您必须稍微调整一下,以便返回字符串而不是对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多