自定义参数文件错误

时间:2015-02-25 00:29:37

标签: php symfony dependency-injection

由于Sensio Insight的限制,我试图将参数从app/config/parameters.yml移到应用程序包但继续获取

  

您已请求不存在的参数   " truckee_volunteer.sandbox"

这是因为app/config/config.yml包含(以及来自捆绑包的其他参考文献parameters.yml

imports:
    - { resource: parameters.yml }
    - { resource: @TruckeeVolunteerBundle/Resources/config/parameters.yml }
    ...
    twig:
        ...
        globals:
            sandbox: "%truckee_volunteer.sandbox%"

此尝试基于此SO answer

扩展类

class TruckeeVolunteerExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        /* for Sensio Insight
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        */

        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.xml');

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('parameters.yml');
    }

    public function getAlias()
    {
        return 'truckee_volunteer';
    }

配置类

class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritDoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('truckee_volunteer');

        $rootNode
            ->children()
                ->scalarNode('admin_user')
                    ->defaultValue('admin')
                    ->isRequired()
                    ->cannotBeEmpty()
                ->end()
                ->scalarNode('admin_password')
                    ->isRequired()
                    ->cannotBeEmpty()
                ->end()
                ->arrayNode('admin_email')
                    ->children()
                        ->scalarNode('address')->isRequired()->cannotBeEmpty()
                    ->end()
                ->end()
                ->scalarNode('admin_first_name')
                    ->isRequired()
                    ->cannotBeEmpty()
                ->end()
                ->scalarNode('admin_last_name')
                    ->isRequired()
                    ->cannotBeEmpty()
                ->end()
                ->booleanNode('sandbox')
                    ->defaultFalse()
                    ->isRequired()
                    ->cannotBeEmpty()
                ->end()
            ->end()
        ;

        // Here you should define the parameters that are allowed to
        // configure your bundle. See the documentation linked above for
        // more information on that topic.

        return $treeBuilder;
    }
}

Bundle的参数.yml

truckee_volunteer:
    admin_user: admin
    admin_password: 123Abcd
    admin_email: 
        address: admin@example.com
    admin_first_name: Benny
    admin_last_name: Borko
    sandbox: false

1 个答案:

答案 0 :(得分:0)

似乎在处理parameters.yml之后才会读取包的app/config/config.yml文件。这是通过在bundle的Extension类中放置一个exit("Bailing out of the Extension class!!");命令来确定的。除非app/config/config.yml未包含"%truckee_volunteer.*%"形式的任何参数,否则该命令未运行。

如果我没有这样做,那么就无法在捆绑中定义可替换参数。谁能证实这一点?

进一步测试:

我无法完全解释或理解这种行为。如果捆绑包的parameters.yml文件被修改为

parameters:
    truckee_volunteer.admin_user: admin
    truckee_volunteer.admin_password: 123Abcd
    truckee_volunteer.admin_email.address: admin@bogus.info
    truckee_volunteer.admin_first_name: Benny
    truckee_volunteer.admin_last_name: Borko
    truckee_volunteer.sandbox: false

然后这些参数可在app/config/config.yml中替换。可能还有其他要求,但通过许多试验,我还没有将它们整理出来。

我现在已经退出了这种方法,因为我理解了Sensio Insight约束让我看到这样做。事实证明,可以扩展默认parameters.yml.dist,但如果包含电子邮件地址,则必须指定符合RFC的默认地址。将所有自定义参数放在parameters.yml.dist中更加容易和有效。