Symfony 3.3服务自动配置 [英] Symfony 3.3 services autoconfiguration
问题描述
我正在尝试迁移到symfony 3.3并使用新功能autowire
/autoconfigure
服务:
I'm trying migrate to symfony 3.3 and use new feature autowire
/autoconfigure
services:
所以在services.yml中,我有:
So in services.yml i have:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
# makes classes in src/AppBundle available to be used as services
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Controller,DQL,Form/DataTransformer,Repository}'
我将我的树枝扩展名声明为:
i declare my twig extension as:
AppBundle\Twig\ImageExtension:
arguments:
$env: "%kernel.environment%"
和该服务的构造函数:
public function __construct(TokenStorage $token, UserRepository $userRepository, RedisCacheService $cache, string $env)
{
$this->env = $env;
$this->user = $token->getToken() ? $token->getToken()->getUser() : false;
$this->userRepository = $userRepository;
$this->cache = $cache;
}
似乎一切正常,但出现此错误:
seems that all is ok, but i'm getting this error:
(1/1) AutowiringFailedException
Cannot autowire service "AppBundle\Twig\ImageExtension": argument "$env" of method "__construct()" must have a type-hint or be given a value explicitly.
,不知道如何解决.
推荐答案
所以如果我尝试使用AppBundle的services.yml,问题就出在这里,如果我理解正确的话,从捆绑包导入服务的旧样式将无法正常工作自动装配/自动配置,因为我们需要从AppExtension
重写load()
方法以使用类型提示.因此,我已将所有服务替换为app/config/services.yml
,这对我有帮助.
So problem was in case that i was trying to use services.yml from AppBundle, if i understand right, old style of importing services from bundles doesn't work with autowiring/autoconfiguring because we need to rewrite load()
method from AppExtension
to use type hints. So i've replaced all my services to app/config/services.yml
and it helps me.
这篇关于Symfony 3.3服务自动配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!