【问题标题】:How to import mapping parameter in Symfony 2 configuration file?如何在 Symfony 2 配置文件中导入映射参数?
【发布时间】:2014-06-30 02:08:35
【问题描述】:

这是我期望的正常工作:

  • parameters.yml

    host:
        main: example.com
    
  • app/config/config.yml

    twig:
        globals:
            hosts_main:   %host.main%
    

但是抛出异常:

[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]  
 You have requested a non-existent parameter "host.main". 

而且我在文档中找不到任何示例。 奇怪的。 例如,Incenteev\ParameterHandler\ScriptHandler::buildParameters() 如果 .dist 文件中有一个,并且两个结构在 YAML 方面相等,则用映射替换“流”表示法,但 Symfony 本身似乎不理解 YAML。

谁能建议一些适当的解决方法。

【问题讨论】:

    标签: symfony parameters yaml configuration-files


    【解决方案1】:

    参数键丢失,它应该是扁平的:

    parameters:
        host.main: example.com
    

    或者

    parameters:
        hosts:
            main: example.com
            other: example.com
    
    twig:
        globals:
            hosts: "%hosts%"
    

    【讨论】:

    • “参数:”在那里。我只是没有粘贴整个文件。
    • 我不需要它平坦。点符号表示 Twig 中的属性访问,我想将参数导入 Twig 而无需太多转换。那是另一个故事。但基本上我认为 YAML 应该保留 YAML 而不是一些 pidgin。
    • 为什么还需要 host.main?是请求中的主机吗?你可以在twig中使用app.request.host,如果你想在控制台中设置see,还有一个router.request_context.host参数。
    • 变量名无关紧要。我有几十个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 2019-11-02
    相关资源
    最近更新 更多