2016-09-08 14 views
-1

我想在我的Bundle的服務配置的標記定義中放置一個類名(用戶已在app/config/config.yml中配置)。在服務的標記定義中使用config.yml參數

app/config/config.yml

my: 
    class_name: AppBundle\Entity\Product 

我的包的配置:services.xml

<service id="my.entity_listener" class="My\EventListener\MyListener"> 
    <tag name="doctrine.orm.entity_listener" entity="%my.class_name%" event="preUpdate" /> 
</service> 

這有可能以任何方式?我所能得到的只是%my.class_name%。它不會在標籤定義中被替換。雖然,我的依賴注入的工作,因爲它取代了它也只是以上,在同一個配置文件:

<service id="my.param_converter" class="My\ParamConverter\MyParamConverter"> 
    <tag name="request.param_converter" converter="user" priority="10" /> 
    <argument type="service" id="doctrine.orm.default_entity_manager" /> 
    <argument>%my.class_name%</argument> <!-- Replaced by AppBundle\Entity\Product --> 
</service> 
+1

沒有辦法直接使用my.class_name作爲參數,因爲它不是參數。您將需要使用編譯器通行證來修改您的服務定義。可能有點涉及:http://symfony.com/doc/current/bundles/configuration.html和http://symfony.com/doc/current/service_container/compiler_passes.html – Cerad

回答

0

在我的項目,如果我想在全球範圍內config.yml添加一些自定義的參數,我做這樣的事情:

parameters: 
    my: 
     - {class_name: "AppBundle\Entity\Product"} 
+0

你把你的所有配置bundles在parameters.yml中?在這種情況下你如何使用驗證?如何使用你的捆綁軟件的人知道他們必須把它的配置放在他們自己的parameters.yml文件中? – Leogout

-1

也許嘗試使用parameters代替my這樣的:

parameters: 
    class_name: AppBundle\Entity|Product 

,然後用它作爲%class_name%

+0

與以上stuzzo相同的問題: 您是否將所有的yout包的配置都放入了parameters.yml中?在這種情況下你如何使用驗證?如何使用你的捆綁軟件的人知道他們必須把它的配置放在他們自己的parameters.yml文件中? – Leogout

+0

查看Cerad的新評論:http://stackoverflow.com/questions/39384214/use-config-yml-parameters-in-services-tag-definition/39385874?noredirect=1#comment66107979_39384214 - 這就是我的做法它在我的包中(但我沒有將它們中的任何一個作爲開源發佈 - 我們在內部使用它們)。 – Najki

+0

另外,看看其他人如何在一些流行的捆綁包中做到這一點。這裏有NelmioApiDocBundle的[NelmioApiDocExtension.php](https://github.com/nelmio/NelmioApiDocBundle/blob/master/DependencyInjection/NelmioApiDocExtension.php)。正如你所看到的 - 他們從配置中讀取所有內容,並在運行時將它傳遞給參數。 – Najki

相關問題