Symfony2 DI不作为参数传递服务

时间:2015-07-18 13:19:29

标签: symfony dependency-injection

请帮帮我,我完全陷入了困境。

/src/Application/Lexik/TranslationBundle/Resources/config/services.yml

, matches the character , literally
(?=([^\"]*\"[^\"]*\")*[^\"]*$) Positive Lookahead - Assert that the regex below can be matched
1st Capturing group ([^\"]*\"[^\"]*\")*
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
[^\"]* match a single character not present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\" matches the character " literally
\" matches the character " literally
[^\"]* match a single character not present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\" matches the character " literally
\" matches the character " literally
[^\"]* match a single character not present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\" matches the character " literally
$ assert position at end of the string

的src /应用/ Ibrows / SonataTranslationBundle / DependencyInjection /编译器/ ReplaceCrudControllerPass.php

parameters:
    lexik_translation.locale.loader.class: Application\Lexik\TranslationBundle\LocalesLoader\OrmLocalesLoader
    lexik_translation.locale.entity: Application\Lexik\TranslationBundle\Entity\Locale

services:
    lexik_translation.locale.repository:
        class: Doctrine\ORM\EntityRepository
        factory_service: doctrine.orm.default_entity_manager
        factory_method: getRepository
        arguments:
            - %lexik_translation.locale.entity%

    lexik_translation.locale.loader:
        class: %lexik_translation.locale.loader.class%
        parameters:
            - @lexik_translation.locale.repository

当我运行任何cli命令或重新加载页面时,出现下一个错误:

namespace Application\Ibrows\SonataTranslationBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class ReplaceCrudControllerPass implements CompilerPassInterface
{
    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container)
    {
        if (!$container->hasDefinition('lexik_translation.locale.loader')) {
            throw new \RuntimeException('The "lexik_translation.locale.loader" configuration required.');
        }

        $definition = $container->getDefinition('ibrows_sonata_translation.admin.orm');
        $definition->setClass('Application\Ibrows\SonataTranslationBundle\Admin\ORMTranslationAdmin');
        $definition->replaceArgument(2, 'ApplicationIbrowsSonataTranslationBundle:TranslationCRUD');
        $definition->addMethodCall('setLocalesLoader', array(new Reference('lexik_translation.locale.loader')));
    }
}

at appDevDebugProjectContainer.php接下来(没有在构造函数中注入@ lexik_translation.locale.repository):

[Symfony\Component\Debug\Exception\ContextErrorException]                                                                                                                         
Catchable Fatal Error: Argument 1 passed to Application\Lexik\TranslationBundle\LocalesLoader\OrmLocalesLoader::__construct() must be an instance of Doctrine\ORM\EntityRepository, none given, called in /private/var/www/odesk/translate-admin/app/cache/dev/appDevDebugProjectContainer.php on line 2385 and defined                                           

为什么这很重要?有谁知道吗?

1 个答案:

答案 0 :(得分:0)

我只是有一个错字。并在发布问题时意识到。 需要在/** * Gets the 'lexik_translation.locale.loader' service. * * This service is shared. * This method always returns the same instance of the service. * * @return \Application\Lexik\TranslationBundle\LocalesLoader\OrmLocalesLoader A Application\Lexik\TranslationBundle\LocalesLoader\OrmLocalesLoader instance. */ protected function getLexikTranslation_Locale_LoaderService() { return $this->services['lexik_translation.locale.loader'] = new \Application\Lexik\TranslationBundle\LocalesLoader\OrmLocalesLoader(); } 通过arguments:,而不是parameters:。正确的是:

services.yml