gpt4 book ai didi

php - Symfony 3.3 服务自动配置

转载 作者:可可西里 更新时间:2023-11-01 13:20:37 26 4
gpt4 key购买 nike

我正在尝试迁移到 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}'

我将我的 Twig 扩展声明为:

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.

不知道如何解决。

最佳答案

我有相同的错误消息,但是由不同的错误引起的。也许有人会发现这很有用。

我在 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

关于php - Symfony 3.3 服务自动配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44616346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com