Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sf2 Importing bundle services.yml from subdirectory

Tags:

yaml

symfony

Is there a possibility to import a services.yml file from a subdir in config?

I've got a structure like this:

[Acme/MyBundle/Resources]

--[config]

----[routing]

----[services]

------[User]

--------services.yml

----[validation]

----routing.yml

----services.yml

----validation.yml

Now I want to import a file Acme/MyBundle/Resources/config/services/User/services.yml into Acme/MyBundle/Resources/config/services.yml, which is loaded in DependencyInjection/AcmeMyExtension. The code in Acme/MyBundle/Resources/config/services.yml is:

imports:
    - { resource: "@AcmeMyBundle/Resources/config/services/User/services.yml" }

services:
    //other services

I'm getting error:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'The file "@AcmeMyBundle/Resources/config/services/User/services.yml" does not exist (in: ., C:\wamp\www\dir\src\Acme\MyBundle\DependencyInjection/../Resources/config).' in C:\wamp\www\dir\app\cache\dev\classes.php:1518 
Stack trace: #0 C:\wamp\www\dir\vendor\symfony\symfony\src\Symfony\Component\Config\Loader\FileLoader.php(70): Symfony\Component\Config\FileLocator->locate('@AcmeMyBundle/...', '.', false) #1 C:\wamp\www\dir\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Loader\YamlFileLoader.php(97): Symfony\Component\Config\Loader\FileLoader->import('@AcmeMyBundle/...', NULL, false, 'services.yml') #2 C:\wamp\www\dir\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Loader\YamlFileLoader.php(54): Symfony\Component\DependencyInjection\Loader\YamlFileLoader->parseImports(Array, 'services.yml') #3 C:\wamp\www\dir\src\Acme\MyBundle\DependencyInjection\AcmeMyExtension.php(26): Symfony\Co in C:\wamp\www\dir\vendor\symfony\symfony\src\Symfony\Component\Config\Loader\FileLoader.php on line 100

The code of Acme/MyBundle/DependencyInjection/AcmeMyExtension.php:

<?php

namespace Core\MpgBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * 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 CoreMpgExtension extends Extension
{

    /**
     * {@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');
    }
}
like image 672
thorinkor Avatar asked Jan 10 '14 09:01

thorinkor


2 Answers

I was able to get this working by doing the following:

In services.yml

imports:
    - { resource: 'subdirectory\filename.yml' }

Now, as to why this is the case, instead of the typical @Bundle\Path\To\File? My suspicion is because the DependencyInjection class hasn't completed processing, the framework isn't able to use that annotation to figure out where things reside. Those more knowledgeable about internals can confirm.

like image 128
ReservedDeveloper Avatar answered Oct 26 '22 13:10

ReservedDeveloper


It is possible to import other ressources in yml file, even if the files you would like to import are in subfolder.

For example, accept you have a file named config.yml and you would like to import others yml file under subfolder named assets, you could import your differents yml file like this:

#config.yml file
imports:
    - { resource: assets/design.yml }
    - { resource: assets/params.yml }

You can also import entire directories to load all the resources inside them:

#config.yml file
imports:
    - { resource: assets/ }

See for example symfony doc here

Hope this could help you.

like image 1
french_dev Avatar answered Oct 26 '22 13:10

french_dev