Loading custom config file for application in symfony2

I am developing an application symfony2

and I am trying to include my custom yaml config located at/src/AppBundle/Resources/Config/general.yml

I have given the example given here http://symfony.com/doc/current/cookbook/bundles/extension.html and created in a 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 am stuck at this step and have no idea how to get symfony to execute this file and load the config.

+3


source to share


1 answer


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

Assuming 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 to DependencyInjection so it is 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.

+2


source







All Articles