21

I have the following in my config.yml

services:
    my.user_provider:
        class: Acme\MySecurityBundle\Security\UserProvider

but would like to move this to config.yml in my MySecurityBundle/Resources/config but Symfony2 tells me that the service doesn't exist when I move it.

How do I get it to pick up the config.yml file from there?

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
josef.van.niekerk
  • 11,941
  • 20
  • 97
  • 157

3 Answers3

40

src/Acme/MySecurityBundle/DependencyInjection/MySecurityExtension.php:

<?php
namespace Acme\MySecurityBundle\DependencyInjection;

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

class MySecurityExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}

src/Acme/MySecurityBundle/Resources/config/services.yml:

services:
    my_security.user_provider:
        class: Acme\MySecurityBundle\Security\UserProvider
Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
21

I accomplished this by referencing it as an import in app/config.yml:

imports:
    - { resource: "@MySecurityBundle/Resources/config/services.yml" }
Thomas Kelley
  • 10,187
  • 1
  • 36
  • 43
  • 3
    This is what I used to do, but I'm looking for a way to define the service without making changes to app/config.yml. Thanks for answering! – josef.van.niekerk Nov 30 '11 at 07:58
16

You need to create a class in that bundle called an 'extension' that tells Symfony what to do when loading the bundle. The naming convention is a little weird. For Acme\MySecurityBundle, the class will be named AcmeMySecurityExtension. It lives in {bundlepath}/DependencyInjection.

Here is an example of one of mine (I am loading Resources/config/services.xml):

<?php

namespace Acme\MySecurityBundle\DependencyInjection;

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

/**
 * This class is automatically discovered by the kernel and load() is called at startup.
 * It gives us a chance to read config/services.xml and make things defined there available for use.
 * For more information, see http://symfony.com/doc/2.0/cookbook/bundles/extension.html
 */
class AcmeMySecurityExtension extends Extension
{
    /**
     * Called by the kernel at load-time.
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        /*@var XmlFileLoader*/
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.xml');
    }
}
tuxedo25
  • 4,798
  • 1
  • 16
  • 12