Symfony 3.3 Services Auto Config

I am trying to upgrade to symfony 3.3 and use the new autowire/ autoconfigureservices function :

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 removed anyway
        exclude: '../../src/AppBundle/{Entity,Controller,DQL,Form/DataTransformer,Repository}'

I declare my branch extension as:

AppBundle\Twig\ImageExtension:
      arguments:
          $env: "%kernel.environment%"

and constructor for this service:

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;
}

Everything seems to be in order, but I get 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.

and don’t know how to fix it.

+4
source share
2 answers

, , services.yml AppBundle, , autowiring/autoconfiguring, load() AppExtension . app/config/services.yml, .

+1

, . , - .

service.yml :

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

:

"AppBundle\Service\MyService": "$ foobar" "__construct()" - .

:

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

app.my_service:
    alias: AppBundle\Service\MyService
    public: true
+2

Source: https://habr.com/ru/post/1679563/


All Articles