简体   繁体   English

Symfony 4中的自定义YAML配置文件

[英]Custom YAML configuration file in Symfony 4

I'm trying to create a custom configuration file outside of config/packages , as I need this file to be writable. 我正在尝试在config/packages之外创建自定义配置文件,因为我需要此文件可写。 I understand that Symfony 4 does not define an AppBundle like previous versions. 我了解Symfony 4并未像以前的版本那样定义AppBundle。 I've seen similar issues with solutions that does not work for my specific case. 我在解决方案中遇到了类似问题,这些问题不适用于我的特定情况。

So far I have created the following files: 到目前为止,我已经创建了以下文件:

# config/settings.yaml

settings:
    foo: 3
<?php

namespace App\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $tree_builder = new TreeBuilder();
        $root_node = $tree_builder->root('settings');

        $root_node
            ->addDefaultsIfNotSet()
            ->children()
                ->scalarNode('foo')
                    ->defaultValue(1)
                ->end()
            ->end()
        ;

        return $tree_builder;
    }
}
<?php

namespace App\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class SettingsExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
        $loader->load('settings.yaml');

        $container->setParameter('settings.foo', $config['foo']);
    }
}

And I've loaded the extension in Kernel.php as: 我已经在Kernel.php中将扩展加载为:


    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
    {
        $container->registerExtension(new SettingsExtension());
        $container->loadFromExtension('settings');

        ...
    }

But if get the following error: 但是如果出现以下错误:

There is no extension able to load the configuration for "settings" (in src/DependencyInjection/../../config/settings.yaml). Looked for namespace "settings", found none

You take the problem by the wrong end. 您错误地解决了问题。

as I need this file to be writable 因为我需要此文件可写

If some along the (deployement?) processes you want to modify configuration before cache generation, the permission problem is an OPS problem. 如果某些(部署?)过程中您想要在生成缓存之前修改配置,则权限问题就是OPS问题。 Surely you can't find a way to put the config/services.yml writable by using the right permission system. 当然,您找不到使用正确的权限系统将config / services.yml设置为可写的方法。
Parameters files were replaced by .env file and environnement variables, you could write them? 参数文件被.env文件和环境变量替换,您可以编写它们吗?

If the mean is to put change global parameters it would be so much easier via database (looks at CraueConfigBundle ). 如果要更改全局参数的话,那么通过数据库将更加容易(请参见CraueConfigBundle )。
Yaml files are putted in cache. Yaml文件放入缓存中。 it means dynamically write yaml will fail in prod. 这意味着动态写入yaml将在prod中失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM