Symfony 3.3 services autoconfiguration
Asked Answered
E

2

6

I'm trying migrate to symfony 3.3 and use new feature autowire/autoconfigure services:

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%"

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

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.

and have no idea how to fix it.

Ensoll answered 18/6, 2017 at 15:4 Comment(6)
Alas, autowire only works with classes. No strings or integers. You could make a class that wraps $env but this whole autowire fad is not going to last. #44387603Arbor
@Ensoll doesn't talk about autowiring the env argument, he talks about his configuration that does not work. Based on the official documentation about manually wiring, his configuration looks correct. @Ensoll : Are you sure you update your Symfony dependencies up to 3.3+ ?Rally
@NoémiSalaün that's rightEnsoll
Maybe you should try to fully configure the arguments, and see if you can find an explanation about why it doesn't work. You can also try to configure it by index, 3 instead of $env. Or used empty quotes for the others 3 services. argument: ['', '', '', '%kernel.environment%]Rally
Did you ever find a way to pass a parameter using autowire? Just curious.Arbor
@Cerad, check my answer.Ensoll
E
2

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.

Ensoll answered 22/6, 2017 at 15:22 Comment(3)
I have very similar problem, but I don't understand what exactly did you do. Can you please explain the answer little bit more? Thanks.Fading
@Fading hey, just try to move configuration from service.yml (ex. AppBundle) to service.yml in app/configEnsoll
thanks for quick reply. My service.yml was in app/config/ all the time, and still it didn't work. In the meantime I found the solution for my problem. I am unable to format the multiline code block in the comment so I will add it as answer. I hope you don't mind.Fading
F
4

I had same error message, but caused by different bug. Maybe somebody will find this useful.

My initial config in service.yml was:

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

And I was getting this error:

Cannot autowire service "AppBundle\Service\MyService": argument "$foobar" of method "__construct()" must have a type-hint or be given a value explicitly.

Then few hours later I found the solution:

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

app.my_service:
    alias: AppBundle\Service\MyService
    public: true
Fading answered 24/12, 2017 at 16:21 Comment(0)
E
2

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.

Ensoll answered 22/6, 2017 at 15:22 Comment(3)
I have very similar problem, but I don't understand what exactly did you do. Can you please explain the answer little bit more? Thanks.Fading
@Fading hey, just try to move configuration from service.yml (ex. AppBundle) to service.yml in app/configEnsoll
thanks for quick reply. My service.yml was in app/config/ all the time, and still it didn't work. In the meantime I found the solution for my problem. I am unable to format the multiline code block in the comment so I will add it as answer. I hope you don't mind.Fading

© 2022 - 2024 — McMap. All rights reserved.