在symfony 2中将bundle注入服务

时间:2012-09-19 08:29:41

标签: symfony dependency-injection bundle

有没有办法将捆绑注入服务?

我想写这样的服务构造函数:

<?php
use Symfony\Component\HttpKernel\Bundle\Bundle;

class MyService
{
    /** @var Bundle */
    private $bundle;

    public function __construct(Bundle $bundle) 
    {
        $this->bundle = $bundle;
    }
}

对于services.yml我希望有类似的内容:

services:
    my_service:
        class:     MyService
        arguments: ['how_can_i_reference_a_bundle']

1 个答案:

答案 0 :(得分:7)

您的捆绑包可以设置为以下服务:

services:
    acme_foo_bundle:
        class: Acme\Foo\AcmeFooBundle
        factory_service: kernel
        factory_method: getBundle
        arguments:
            - "AcmeFooBundle"