Symfony 3.4 : Cannot autowire service : argument of method "__construct()" has no type-hint, you should configure its value explicitly

I’m trying to define service using Symfony 3.4 DI but I got the following error :

Cannot autowire service “AppBundleServiceSelectionWeb”: argument “$config” of method “__construct()” has no type-hint, you should configure its value explicitly.

..But I did configure its value.

Here are my conf files:

AppBundleServiceMyservice

<?php
public function __construct(Security $security, SessionInterface $session, EntityManagerInterface $em, $config)
{
}

AppBundleRessourcesconfigservices.yml

myservice:
    public: true
    class: AppBundleServiceMyservice
    arguments:
        $config: 'test'

I also tried:

myservice:
    public: true
    class: AppBundleServiceMyservice
    arguments:
        $config: '%config%' # %config% defined in AppBundle/Ressource/config/parameter.yml as array

and

myservice:
    public: true
    class: AppBundleServiceMyservice
    bind:
        $config: "@=parameter('config')"

Thanks to @Cerad it works now using this:

AppBundleServiceMyservice:
    # public mean if service can be get from container
    # if false consider using DI
    public: true
    autowire: true
    arguments:
        $config: '%config%'

The remaining question is why it doesn’t affect all services ? I have more than one service where I inject $config as argument using bind and there is no issues

AppBundleServiceSearchEngine

public function __construct(RequestStack $requestStack, EntityManagerInterface $em, TranslatorInterface $translator, Tools $tools, array $config)

AppBundleressourcesconfigservices.yml

services:
    searchEngine: # no problem here when inject $config manually
        public: true
        class: AppBundleServiceSearchEngine
        autowire: true
        bind:
            $config: "@=parameter('config')"

    #before not working
    myservice:
        public: true
        class: AppBundleServiceMyservice
        autowire: true
        arguments:
            $config: '%config%'

    #After working
    AppBundleServiceMyservice:
        public: true
        autowire: true
        arguments:
            $config: '%config%'

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *