Symfony 3.3 服务自动配置
Symfony 3.3 services autoconfiguration
我正在尝试迁移到 symfony 3.3 并使用新功能 autowire
/autoconfigure
服务:
所以在 services.yml 我有:
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}'
我将我的树枝扩展声明为:
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;
}
似乎一切正常,但我收到此错误:
(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,如果我理解正确的话,从捆绑包导入服务的旧样式不适用于 autowiring/autoconfiguring,因为我们需要重写 AppExtension
中的 load()
方法以使用类型提示。所以我已经将所有服务替换为 app/config/services.yml
,它对我有帮助。
我有同样的错误信息,但是由不同的错误引起的。也许有人会觉得这很有用。
我在 service.yml 中的初始配置是:
app.my_service:
class: 'AppBundle\Service\MyService'
arguments:
$foobar: 'some value for foobar'
public: true
我收到了这个错误:
Cannot autowire service "AppBundle\Service\MyService": argument
"$foobar" of method "__construct()" must have a type-hint or be given
a value explicitly.
然后几个小时后我找到了解决方案:
AppBundle\Service\MyService:
arguments:
$foobar: 'some value for foobar'
app.my_service:
alias: AppBundle\Service\MyService
public: true
我正在尝试迁移到 symfony 3.3 并使用新功能 autowire
/autoconfigure
服务:
所以在 services.yml 我有:
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}'
我将我的树枝扩展声明为:
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;
}
似乎一切正常,但我收到此错误:
(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,如果我理解正确的话,从捆绑包导入服务的旧样式不适用于 autowiring/autoconfiguring,因为我们需要重写 AppExtension
中的 load()
方法以使用类型提示。所以我已经将所有服务替换为 app/config/services.yml
,它对我有帮助。
我有同样的错误信息,但是由不同的错误引起的。也许有人会觉得这很有用。
我在 service.yml 中的初始配置是:
app.my_service:
class: 'AppBundle\Service\MyService'
arguments:
$foobar: 'some value for foobar'
public: true
我收到了这个错误:
Cannot autowire service "AppBundle\Service\MyService": argument "$foobar" of method "__construct()" must have a type-hint or be given a value explicitly.
然后几个小时后我找到了解决方案:
AppBundle\Service\MyService:
arguments:
$foobar: 'some value for foobar'
app.my_service:
alias: AppBundle\Service\MyService
public: true