依赖注入 - Symfony(没有扩展能够加载&#34的配置; acme_api.question_manager"(在../Resources/config/services.yml中)

时间:2015-05-01 05:46:04

标签: php symfony dependency-injection

在symfony2中使用DI设置新服务时出现此错误 我正在使用symfony2.3并使用fos-rest bundle来创建json API'

  • 以下是我的代码的样子

DependencyInjection /的configuration.php

<?php

namespace Acme\ApiBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * This is the class that validates and merges configuration from your     app/config files
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
 */
class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritDoc}
     *
     * @return TreeBuilder
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $treeBuilder->root('acmeapi');


        // Here you should define the parameters that are allowed to
        // configure your bundle. See the documentation linked above for
        // more information on that topic.

        return $treeBuilder;
    }
}

DependencyInjection / AcmeApiExtension.php

<?php

namespace Acme\ApiBundle\DependencyInjection;

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

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

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }

    public function getAlias()
    {
        return 'acme_api';
    }
}

我的Resources / config / services.yml

parameters: 
    # Overriding Security Bundle's Access Listener class to provide  detailed
    # error message
services:
acme_api.question_manager:
    class: AcmeApiBundle\Manager\QuestionManager
    arguments:
      - @doctrine
      - @validator

最后是app / confog / config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: @AcmeApiBundle/Resources/config/services.yml }
.......

另外,添加appkernel文件

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new Acme\ApiBundle\AcmeApiBundle(),
            new JMS\SerializerBundle\JMSSerializerBundle(),
            new FOS\RestBundle\FOSRestBundle(),
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    { 
        $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
    }
}

如果需要更多数据点,请告诉我们,以便在此处提供帮助。

1 个答案:

答案 0 :(得分:1)

您正在混合config.yml和services.yml。第一个包含bundle配置,第二个包含服务和参数。

services.yml已加载到行class AcmeApiExtension的{​​{1}}中,因此无需再次加载:

$loader->load('services.yml');

(带扩展的加载服务是捆绑包的最常用方式)。有关其他信息,请查看:http://symfony.com/doc/current/book/service_container.html#importing-other-container-configuration-resources

services.yml中也存在格式错误。纠正一个:

- { resource: @AcmeApiBundle/Resources/config/services.yml }

(最后5行的缩进)。这会导致“无延期”错误。