【问题标题】:Adding items to Symfony2 parameters array将项目添加到 Symfony2 参数数组
【发布时间】:2016-04-07 20:42:09
【问题描述】:

我有一个由许多捆绑包组成的 Symfony2 (2.8.1) 应用程序。 我想要一个“关于”页面,显示每个捆绑包的版本。 为此,我尝试使用这样的参数数组:

revision:
  'bundle1':
    version: 1.0.0
    build: 42
  'bundle2':
    version: 1.2.0
    build: 4242

等等。 如果我将所有内容都放在一个 YAML 文件中,它就可以工作。 相反,我希望每个包都定义其修订版。就像是: bundle1的参数文件:

revision:
  'bundle1':
    version: 1.0.0
    build: 42

bundle2的参数文件:

revision:
  'bundle2':
    version: 1.2.0
    build: 4242

如果我这样做,则修订数组仅填充最后包含的 yaml 文件的值。 有没有办法将条目“添加”到不同 yaml 文件中定义的参数数组? BR 斯特凡诺

【问题讨论】:

    标签: php arrays symfony parameters yaml


    【解决方案1】:

    我改变了方法并解决了问题。 我没有将修订信息添加到配置中,而是简单地在每个包的 config 文件夹中创建一个 Yaml 文件。 然后我将 yaml 文件内容加载到一个数组中,以便在 about 页面中使用。

    代码类似于:

        # first place to check is app config folder
        $kernel_root=$this->get('kernel')->getRootDir();
        $toLocate[]=$kernel_root.'/config';
    
        # then get config folder for each active bundle
        $kernel = $this->get('kernel');
        $configDirectories=$this->getParameter('kernel.bundles');
        foreach ($configDirectories as $bundleName => $configDirectory) {
            $toLocate[]= $kernel->locateResource('@'.$bundleName).'Resources/config';
        }
    
        # now get array of revision files
        $locator = new FileLocator($toLocate);
        $yamlRevFiles = $locator->locate('revision.yml', null, false);
    
        # and finally create array of date read from revision files
        $revs=array();
        foreach ($yamlRevFiles as $yamlRevFile) {
            $revs[]=Yaml::parse($yamlRevFile);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多