문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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

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

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');
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top