3

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.

2
  • Content of your general.yml ? Commented Aug 1, 2015 at 19:50
  • Mind your syntax, you're opening the FileLocator constructor with a double quote and are closing it with a single quote. Commented Aug 1, 2015 at 19:53

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.