2013-02-13 75 views
4

我正在尋找創建我自己的yml文件來存儲一些全局設置和變量,我可以在我的項目中重新使用它們。我已經在這裏搜查,發現另一個答案然而symfony2如何創建自己的自定義yml文件以全局共享?

Set own parameters in symfony2

我期待加入report_settings.yml文件,以便它在我的項目中自動加載它並沒有爲我工作。

這是我迄今基於

#app/config/config.yml 
imports: 
    - { resource: parameters.yml } 
    - { resource: security.yml } 
    - { resource: report_settings.yml } 

.. rest of config file -- 

和文件即時試圖包括看起來像這樣

#report_settings.yml 
something: 
    something1: "test" 

它返回以下錯誤

FileLoaderLoadException: Cannot import resource "/var/www/pcPortal/app/config/report_settings.yml" from "/var/www/pcPortal/app/config/config.yml". 

InvalidArgumentException: There is no extension able to load the configuration for "something" (in /var/www/pcPortal/app/config/report_settings.yml). Looked for namespace "something", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "jms_aop", "jms_di_extra", "jms_security_extra", "sj_query", "web_profiler", "sensio_distribution" 

從閱讀錯誤信息看起來我錯過了什麼?我是否需要添加某種擴展?

+0

我的問題可能是有用的 - HTTP:// stackoverflow.com/questions/32482622/creating-a-configuration-file-in-symfony2/32502618#32502618 – Peter 2015-09-10 14:26:21

回答

7

最簡單的是把你的配置到像參數部分:

#report_settings.yml 
parameters: 
    something.something1: "test" 

你也可以處理與自己的分機配置看http://symfony.com/doc/master/book/service_container.html#importing-configuration-via-container-extensions

+0

這工作。獲取var我只需要做$ this-> container-> getParameter('something.test') – 2013-02-14 09:43:54

+0

如何從實體內部調用$ this-> container-> getParameter()? – 2013-02-14 15:15:09

+0

您必須將所需的值傳遞給實體,例如$ report = new Report($ this-> container-> getParameter('something.test'))'或通過setter。您還可以將報告配置爲容器中的服務。 – 2013-02-14 20:32:38

相關問題