2012-09-19 59 views
0

有沒有辦法將一個bundle注入到服務中?在symfony 2中注入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

我需要收集每包的對象。當然,我可以用一些字符串標識符來做到這一點,但我更願意使用已經存在的東西。但我不知道如何獲得捆綁。 – BetaRide

+2

我很確定你做錯了。你需要收集哪些物品?其他服務?閱讀使用標記的服務:http://symfony.com/doc/current/components/dependency_injection/tags.html –

回答

7

你的包可以被設置爲服務像這樣:

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

非常好的主意! – BetaRide