Symfony 4参数没有类型提示,您应该显式配置其值

时间:2019-02-22 10:39:02

标签: service autowired symfony-4.2

Symfony 4.2.3

最近从3.4版升级到4.2.3,并使我的项目正常运行,但是 在services.yaml中将autoconfigure设置为true时,我将收到此错误消息:

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

我的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}

我的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);
        }
    }
}

我尝试过的事情:

  1. 在RedirectToLocaleActiveListener的__construct中向$ localActive添加“字符串”

结果:

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

2 个答案:

答案 0 :(得分:0)

标量类型的参数不能自动连接。您需要手动接线。

您可以尝试在服务定义中显式连接参数:

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

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

或者您可以利用本地服务绑定功能将参数绑定到标量参数:

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

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

答案 1 :(得分:0)

如果您的服务名称不等于fqcn,例如:

    app.ext.telegram_bot_api:
      class: 'App\Ext\TelegramBot\Bot'

以及您使用自动服务解析的位置,如下所示:

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Document,Migrations,Tests,Kernel.php}'

您应该这样在您的服务名称和fqcn之间创建别名:

'App\Ext\TelegramBot\Bot': '@app.ext.telegram_bot_api'

因此您的自动服务解析应了解您的服务额外配置。