6

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

并且不知道如何解决它。

4

2 回答 2

4

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

我在 service.yml 中的初始配置是:

app.my_service:
    class: 'AppBundle\Service\MyService'
    arguments:
        $foobar: 'some value for foobar'
    public: true

我收到了这个错误:

无法自动装配服务“AppBundle\Service\MyService”:方法“__construct()”的参数“$foobar”必须具有类型提示或显式指定值。

然后几个小时后,我找到了解决方案:

AppBundle\Service\MyService:
    arguments:
        $foobar: 'some value for foobar'

app.my_service:
    alias: AppBundle\Service\MyService
    public: true
于 2017-12-24T16:21:37.783 回答
2

所以问题是,如果我试图使用 AppBundle 中的 services.yml,如果我理解正确,从包中导入服务的旧样式不适用于自动装配/自动配置,因为我们需要重写load()方法 fromAppExtension以使用类型提示。所以我已经更换了我所有的服务app/config/services.yml,这对我有帮助。

于 2017-06-22T15:22:54.930 回答