在 symfony2 中为应用程序加载自定义配置文件 [英] Loading custom config file for app in symfony2

查看:26
本文介绍了在 symfony2 中为应用程序加载自定义配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 symfony2 应用程序,我正在尝试包含位于 /src/AppBundle/Resources/Config/general.yml

我遵循了此处提供的示例 http://symfony.com/doc/current/cookbook/bundles/extension.html 并在 src/AppBundle/DependencyInjection/AppExtension.php 文件中创建,内容如下:

load('general.yml');}}

然而,我卡在了这一点上,不知道如何让 symfony 执行这个文件并加载配置.

解决方案

由于我没有看到您的 general.yml 文件的内容,我建议您使用以下内容(我还没有测试过,但应该没事).

假设这是你的general.yml

学说:形式:实体经理:加密:映射:我的加密捆绑:目录:实体类型:注释前缀:My\EncryptBundle\Entity

您可以直接在 DependencyInjection 中设置所有这些文件,而不是创建此 yml 文件并导入它,这样它就会像下面这样.

命名空间 Application\FrontendBundle\DependencyInjection;使用 Symfony\Component\DependencyInjection\ContainerBuilder;使用 Symfony\Component\Config\FileLocator;使用 Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;使用 Symfony\Component\HttpKernel\DependencyInjection\Extension;使用 Symfony\Component\DependencyInjection\Loader;使用 Symfony\Component\Validator\Tests\Fixtures\Entity;/*** 这是加载和管理您的包配置的类** 要了解更多信息,请参阅 {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}*/类 ApplicationFrontendExtension extends Extension 实现 PrependExtensionInterface{/*** {@inheritdoc}*/公共函数加载(数组 $configs,ContainerBuilder $container){$configuration = new Configuration();$config = $this->processConfiguration($configuration, $configs);$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));$loader->load('services.yml');#你的另一个文件$loader->load('controllers.yml');#你的另一个文件$loader->load('repositories.yml');#你的另一个文件}公共函数前置(ContainerBuilder $container){$container->prependExtensionConfig('教义',['orm' =>['entity_managers' =>['加密' =>['映射' =>['MyEncryptBundle' =>['目录' =>'实体','类型' =>'注解','前缀' =>'我的\加密捆绑\实体']]]]]]);}}

或者你可以做点什么像这样.

I am developing a symfony2 application and I am trying to include my custom yaml config located in /src/AppBundle/Resources/Config/general.yml

I have followed example provided here http://symfony.com/doc/current/cookbook/bundles/extension.html and created in src/AppBundle/DependencyInjection/AppExtension.php file with the following content:

<?php
namespace AppBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Translation\Loader\YamlFileLoader;

class AppExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader(
        $container,
        new FileLocator("@AppBundle/Resources/config')
        );
        $loader->load('general.yml');
    }
}

However, I stuck at this point and don't know how to make symfony execute this file and load the config.

解决方案

Since I don't see the content of your general.yml file I can suggest you to use something like below (I haven't tested it but it should be fine).

Assuming that this is your general.yml

doctrine:
    orm:
        entity_managers:
            encrypt:
                mappings:
                    MyEncryptBundle:
                        dir: Entity
                        type: annotations
                        prefix: My\EncryptBundle\Entity

Instead of creating this yml file and importing it, you can directly set all of it in your DependencyInjection so it would be something like below.

namespace Application\FrontendBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Validator\Tests\Fixtures\Entity;

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class ApplicationFrontendExtension extends Extension implements PrependExtensionInterface
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml'); # another file of yours
        $loader->load('controllers.yml'); # another file of yours
        $loader->load('repositories.yml'); # another file of yours
    }

    public function prepend(ContainerBuilder $container)
    {
        $container->prependExtensionConfig(
            'doctrine',
            [
                'orm' => [
                    'entity_managers' => [
                        'encrypt' => [
                            'mappings' => [
                                'MyEncryptBundle' => [
                                    'dir'       => 'Entity',
                                    'type'      => 'annotation',
                                    'prefix'    => 'My\EncryptBundle\Entity'
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        );
    }
}

Or you can do something like this instead.

这篇关于在 symfony2 中为应用程序加载自定义配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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