我正在将Symfony 2.8升级到3.4 LTS。使用注入到TranslatorTrait
中的BreadCrumbExtension
时出现以下错误。
In TraitManager.php line 28:
[Symfony\Component\Debug\Exception\ContextErrorException]
User Error: Value has not been set for AppBundle\Traits\TranslatorTrait
Exception trace:
AppBundle\Traits\TraitManager::_traitValue() at /private/var/www/crmpicco/symfony/src/AppBundle/Traits/TranslatorTrait.php:25
AppBundle\Twig\BreadCrumbExtension->getTranslator() at /private/var/www/crmpicco/symfony/src/AppBundle/Twig/BreadCrumbExtension.php:45
AppBundle\Twig\BreadCrumbExtension->__construct() at /private/var/www/cache/dev/ContainerGijxtqp/appDevDebugProjectContainer.php:1501
ContainerGijxtqp\appDevDebugProjectContainer->getTwigService() at /private/var/www/cache/de_/ContainerGijxtqp/getTwig_CacheWarmerService.php:9
ContainerGijxtqp\appDevDebugProjectContainer->{closure}() at /private/var/www/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/ServiceLocator.php:64
Symfony\Component\DependencyInjection\ServiceLocator->get() at /private/var/www/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php:63
Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheCacheWarmer->warmUp() at /private/var/www/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php:52
Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp() at /private/var/www/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:222
Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand->warmup() at /private/var/www/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:134
Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand->execute() at /private/var/www/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:255
Symfony\Component\Console\Command\Command->run() at /private/var/www/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:964
Symfony\Component\Console\Application->doRunCommand() at /private/var/www/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:86
Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /private/var/www/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:248
Symfony\Component\Console\Application->doRun() at /private/var/www/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:74
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /private/var/www/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:148
Symfony\Component\Console\Application->run() at /private/var/www/crmpicco/symfony/bin/console:27
cache:clear [--no-warmup] [--no-optional-warmers] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>
services.yml
:
服务:
_defaults:
# automatically injects dependencies in your services
autowire: true
autoconfigure: true
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../../../src/AppBundle/{Entity,Repository}'
AppBundle\Twig\BreadCrumbExtension:
arguments:
- '@service_container'
- '@request_stack'
autowire: false
calls:
- [setTranslator, ['@translator']]
我尝试将服务切换到autowire: false
,但没有任何效果。我的服务定义有什么问题?
TranslatorTrait.php
:
namespace AppBundle\Traits;
use Symfony\Component\Translation\TranslatorInterface;
trait TranslatorTrait
{
/**
* @param TranslatorInterface $translator
*/
public function setTranslator(TranslatorInterface $translator)
{
TraitManager::_traitValue(__TRAIT__, $translator);
}
/**
* @return TranslatorInterface
*/
public function getTranslator(): TranslatorInterface
{
return TraitManager::_traitValue(__TRAIT__);
}
}
答案 0 :(得分:0)
此问题的解决方案是重构TraitManager
并按如下所示在每个特征内使用@required
批注:
<?php
namespace AppBundle\Traits;
use Symfony\Component\DependencyInjection\ContainerInterface;
trait ContainerInterfaceTrait
{
/**
* @var ContainerInterface
*/
protected $containerInterface;
/**
* @required
* @param ContainerInterface $value
*/
public function setContainerInterface(ContainerInterface $value)
{
$this->containerInterface = $value;
}
/**
* @return ContainerInterface
*/
public function getContainerInterface(): ContainerInterface
{
return $this->containerInterface;
}
}