在symfony2中为app加载自定义配置文件

时间:2015-08-01 18:59:14

标签: symfony

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

中的自定义yaml配置

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

<?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');
    }
}

但是,我坚持这一点并且不知道如何让symfony执行此文件并加载配置。

1 个答案:

答案 0 :(得分:2)

由于我没有看到你的general.yml文件的内容,我可以建议你使用下面的内容(我没有测试过,但应该没问题。)

假设这是你的general.yml

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

您可以直接在DependencyInjection中设置所有yml文件,而不是创建此yml文件并将其导入,因此它将如下所示。

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'
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        );
    }
}

或者您可以改为like this