在Symfony中创建配置

Pet*_*ter 5 php symfony

几个小时以来,我一直在努力做出你能想象到的最简单的事情而且它不起作用.我已经阅读了大量的stackoverflow问题,阅读有关配置文件的完整Symfony文档以及我阅读的每篇文章或其他信息,它变得越来越难以理解.

细节

我创建了自己的Bundle.让我们来称呼它HappyBundle.我把这个Bundle放在我公司的文件夹里.所以我很自然CompanyHappyBundle.

我想专门为这个bundle创建一个配置文件,因为我希望它可以重用.

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

# src/Company/HappyBundle/Resources/config/config.yml
company_happy:
    import:
        path: /tmp
Run Code Online (Sandbox Code Playgroud)

现在,我想要的是能够在我的控制器中使用此值.我只是不知道如何.它抛出以下错误:

[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").
Run Code Online (Sandbox Code Playgroud)

更新

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

#app/config/config.yml
imports:
    - { resource: "@CompanyHappyBundle/Resources/config/config.yml" }
Run Code Online (Sandbox Code Playgroud)

我也做了一个Configuration类,因为我读到了这个必需的地方.我真的认为这只是一个配置文件的工作.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我实际需要的是实现这一目标所需的步骤和要求.symfony的用处是有百万种方法可以做到这一点.该文档不仅提供工作流程.

有人可以帮帮我吗?

Pet*_*ter 8

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

第一步 - 创建配置文件

创建一个名为文件config.ymlsrc/<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
Run Code Online (Sandbox Code Playgroud)

第二步 - 导入配置文件

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

#app/config/config.yml
imports:
    - { resource: "@YourBundle/Resources/config/config.yml" }
Run Code Online (Sandbox Code Playgroud)

第三步 - 创建配置类

创建一个名为文件Configuration.phpsrc/<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;
    }
}
Run Code Online (Sandbox Code Playgroud)

第四步 - 创建扩展

最后但同样重要的是,您必须创建一个扩展.创建一个文件<yourbundle>Extension.phpsrc/<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);
    }
}
Run Code Online (Sandbox Code Playgroud)

所有这些步骤都只需要能够调用特定于您的包的配置参数.

如果您有任何人知道如何更轻松地做到这一点,请随时发布答案或评论.

  • @Cerad我在整个文档中找到了这个工作流程.应该有一个页面解释如何执行此操作.没有给出任何额外的不必要的形成. (4认同)
  • 在玩完这个后,我发现`$ php app/console generate:bundle`创建了你提供的代码_most_.我只需要将我的特定bundle的配置结构添加到`configuration.php`并将config作为参数暴露在`<bundle> Extension.php`中. (2认同)