在Symfony中创建配置 [英] Creating configurations in Symfony

查看:60
本文介绍了在Symfony中创建配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时以来,我一直在努力做您可以想象的最简单的事情,但这根本行不通.我已经阅读了无数的stackoverflow问题,阅读了有关配置文件的完整Symfony文档,以及阅读的每篇文章或其他信息,都变得越来越难理解.

For a few hours now I've been struggling to do the most simple thing you can imagine and it just won't work. I've read tons of stackoverflow questions, read the complete Symfony documentation on configuration files and with every article or other piece of information I read, it gets harder and harder to understand.

我已经创建了自己的捆绑包.我们称之为HappyBundle.我已将此捆绑包放入公司的文件夹中.所以我自然有CompanyHappyBundle.

I've created my own Bundle. Lets call it HappyBundle. I've put this Bundle in my company's folder. So naturally I've got CompanyHappyBundle.

我想为此捆绑软件专门制作一个配置文件,因为我希望它可以重用.

I want to make a configuration file specifically for this bundle as I want it to be reusable.

我测试时创建了以下内容:

As I test i created the following:

# src/Company/HappyBundle/Resources/config/config.yml
company_happy:
    import:
        path: /tmp

现在,我想要的是能够在Controller中使用此值.我就是不知道它引发了以下错误:

Now, what I want is to be able to use this value in my Controller. I just don't know how. It throws me the following error:

[Symfony\Component\Config\Exception\FileLoaderLoadException]
There is no extension able to load the configuration for "company_happy" (in /home/user/symfony/src/Company/HappyBundle/Resources/config/config.yml).

Looked for namespace "company_happy", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution" in /home/user/symfony/src/Company/HappyBundle/Resources/config/config.yml (which is being imported from "/home/user/symfony/app/config/config.yml").

更新

在config.yml中,我添加了以下内容:

Update

In the config.yml I added the following:

#app/config/config.yml
imports:
    - { resource: "@CompanyHappyBundle/Resources/config/config.yml" }

我也做了一个Configuration类,因为我在某个地方读了这篇文章.我确实确实认为,仅制作一个配置文件是很多工作.

I've also made a Configuration class because I read somewhere this was required. I really do think this is alot of work to make just one config file.

namespace Company\HappyBundle\DependencyInjection;

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

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

        $rootNode
            ->children()
            ->arrayNode('import')
                ->children()
                    ->scalarNode('attachments_path')->defaultValue('/tmp')->end()
                    ->scalarNode('method')->defaultValue('ALL')->end()
                    ->booleanNode('move_mail')->defaultValue(true)->end()
                    ->booleanNode('mark_read')->defaultValue(true)->end()
                ->end()
            ->end()
        ;

        return $treeBuilder;
    }
}

我实际上正在寻找的是实现此工作所需的步骤和要求. symfony的作用在于,它有百万种方法可以做到这一点.该文档不仅提供了工作流程.

What I am actually looking for are the steps and requirements needed to get this working. The thing with symfony is that it has a million ways to do this. The documentation doesn't just give a workflow.

有人可以帮我吗?

推荐答案

我已经解决了自己的问题,但并非没有困难.我对Symfony的配置系统完全不满意.

I have solved my own issue but not without trouble. I'm not at all pleased with Symfony's configuration system.

src/<bundle name>/Resources/config/

yourbundle:
    param_one: value_one
    param_two: value_two
    param_three: value_three
    param_four: value_four
    param_five:
        subparam_one: subvalue_one
        subparam_two: subvalue_two
        subparam_three: subvalue_three
        subparam_four: subvalue_four

第二步-导入配置文件

转到app/config/config.yml并添加:

#app/config/config.yml
imports:
    - { resource: "@YourBundle/Resources/config/config.yml" }

第三步-创建配置类

src/<bundle name>/DependencyInjection/

namespace YourBundle\DependencyInjection;

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

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

        $rootNode
            ->children()
                ->scalarNode('param_one')->defaultValue('value_one')->end()
                ->scalarNode('param_two')->defaultValue('value_two')->end()
                ->scalarNode('param_three')->defaultValue('value_three')->end()
                ->scalarNode('param_four')->defaultValue('value_four')->end()
                ->arrayNode('param_five')
                    ->children()
                        ->scalarNode('subparam_one')->defaultValue('subvalue_one')->end()
                        ->scalarNode('subparam_two')->defaultValue('subvalue_two')->end()
                        ->scalarNode('subparam_three')->defaultValue('subvalue_three')->end()
                        ->scalarNode('subparam_four')->defaultValue('subvalue_four')->end()
                    ->end()
            ->end()
        ;

        return $treeBuilder;
    }
}

第四步-创建扩展名

最后但并非最不重要的一点是,您必须创建一个扩展.在src/<your bundle>/DependencyInjection/

namespace YourBundle\DependencyInjection;

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

class YourbundleExtension extends Extension
{
    /**
     * @var ContainerBuilder
     */
    protected $container;

    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $this->container = $container;

        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        foreach ($config as $key => $value) {
            $this->parseNode('yourbundle.'.$key, $value);
        }

        $container->setParameter('yourbundle', $config);
    }

    /**
     * @param string $name
     * @param mixed  $value
     *
     * @throws \Exception
     */
    protected function parseNode($name, $value)
    {
        if (is_string($value)) {
            $this->set($name, $value);

            return;
        }
        if (is_integer($value)) {
            $this->set($name, $value);

            return;
        }
        if (is_array($value)) {
            foreach ($value as $newKey => $newValue) {
                $this->parseNode($name.'.'.$newKey, $newValue);
            }

            return;
        }
        if (is_bool($value)) {
            $this->set($name, $value);

            return;
        }
        throw new \Exception(gettype($value).' not supported');
    }

    /**
     * @param string $key
     * @param mixed  $value
     */
    protected function set($key, $value)
    {
        $this->container->setParameter($key, $value);
    }
}

所有这些步骤仅是为了能够调用特定于您的捆绑软件的配置参数.

All these steps are required just to be able to call a configuration parameter specific for your bundle.

如果您知道有什么方法可以更轻松地完成此操作,请随时发布答案或评论.

If any of you know any way to do this easier, feel free to post an answer or comment.

这篇关于在Symfony中创建配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆