Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4 argument has no type-hint, you should configure its value explicitly

Symfony 4.2.3

Recently upgraded from version 3.4 to 4.2.3 and got my project working, but when setting autoconfigure in services.yaml to true, I will receive this error message:

Cannot autowire service "App\EventListener\RedirectToLocaleActiveListener": argument "$localeActive" of method "__construct()" has no type-hint, you should configure its value explicitly.

My services.yaml

parameters:
    locale: de
    locale_active: de
    app_locales: de|en
    uploads_directory_name: uploads
    uploads_profile_directory_name: profiles
    uploads_directory: '%kernel.root_dir%/../public/%uploads_directory_name%'
    profile_directory: '%kernel.root_dir%/../public/%uploads_directory_name%/%uploads_profile_directory_name%'
    google_recaptcha_site_key: '%env(GOOGLE_RECAPTCHA_SITE_KEY)%'
services:
    _defaults:
        autowire: true
        autoconfigure: true
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
    App\Controller\:
        resource: ../src/Controller
        tags:
            - controller.service_arguments
    locales:
        class: App\Util\Locales
        arguments:
            - '%locale_active%'
            - '%app_locales%'
            - '@session'
    app.locale:
        class: App\EventListener\LocaleListener
        tags:
            - {name: kernel.event_subscriber}

app.redirect_to_locale_active:
    class: App\EventListener\RedirectToLocaleActiveListener
    arguments:
        - '@router'
        - '%locale_active%'
    tags:
        - {name: kernel.event_subscriber}

My RedirectToLocaleActiveListener.php

<?php

namespace App\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * Class RedirectToLocaleActiveListener
 * When a user enters to the homepage without the parameter locale,
 * the subscriber redirects the user to the main locale.
 *
 * @package App\EventListener
 */
class RedirectToLocaleActiveListener implements EventSubscriberInterface
{
    /**
     * @var UrlGeneratorInterface
     */
    private $urlGenerator;

    /**
     * @var string
     */
    private $localeActive;

    /**
     * @param UrlGeneratorInterface $urlGenerator
     * @param $localeActive
     */
    public function __construct(UrlGeneratorInterface $urlGenerator, $localeActive)
    {
        $this->urlGenerator = $urlGenerator;
        $this->localeActive = $localeActive;
    }

    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::REQUEST => 'onKernelRequest',
        ];
    }

    /**
     * @param GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();

        if ('/' == $request->getPathInfo()) {
            $route = $this->urlGenerator->generate('app_index', ['_locale' => $this->localeActive]);

            $response = new RedirectResponse($route);
            $event->setResponse($response);
        }
    }
}

What I've tried:

  1. adding 'string' to $localActive in __construct of RedirectToLocaleActiveListener

Result:

Cannot autowire service "App\EventListener\RedirectToLocaleActiveListener": argument "$localeActive" of method "__construct()" is type-hinted "string", you should configure its value explicitly.
like image 648
RoyRobsen Avatar asked Feb 22 '19 10:02

RoyRobsen


1 Answers

arguments of scalar type cannot be auto-wired. You need to wire them manually.

You can try wiring the argument explicitly in the service definition:

App\EventListener\RedirectToLocaleActiveListener
    arguments:
        $urlGenerator: '@router'
        $localeActive: '%locale_active%'
    tags:
        - {name: kernel.event_subscriber}

Documentation: https://symfony.com/doc/current/service_container.html#manually-wiring-arguments

Or you can make use of the local service binding feature to bind a parameter to a scalar argument:

services:
    _defaults:
        bind:
            $localeActive: '%locale_active%'

Documentation: https://symfony.com/blog/new-in-symfony-3-4-local-service-binding

like image 90
nod Avatar answered Sep 16 '22 13:09

nod