Symfony 4参数没有类型提示,您应该显式配置其值 [英] Symfony 4 argument has no type-hint, you should configure its value explicitly

查看:91
本文介绍了Symfony 4参数没有类型提示,您应该显式配置其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Symfony 4.2.3

Symfony 4.2.3

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

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.

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

推荐答案

标量类型的参数无法自动连接.您需要手动接线.

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}

文档: 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%'

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

这篇关于Symfony 4参数没有类型提示,您应该显式配置其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆