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

2024-05-05

交响乐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.

我的服务.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 中将“string”添加到 $localActive

Result:

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

标量类型的参数无法自动连接。您需要手动连接它们。

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

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 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 https://symfony.com/blog/new-in-symfony-3-4-local-service-binding

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • [a-zA-Z] 的正则表达式

    我有一个仅匹配英文字母的正则表达式 a a zA Z 字符类 有没有内置的正则表达式 我的意思是像 s or w 您正在要求一个速记班 http www regular expressions info shorthand html对于英文
  • 在 JSON 转换为 CSV 期间保持 JSON 键的顺序

    我正在使用此处提供的 JSON 库http www json org java index html http www json org java index html为了将 json 字符串转换为 CSV 但我遇到的问题是 转换后键的顺序
  • 有效地减去不同形状的 numpy 数组

    使用 numpy 出色的广播规则 您可以减去形状 3 数组v来自形状 5 3 数组X with X v 结果是一个形状 5 3 数组 其中每一行i是有区别的X i v 有没有办法减去形状 n 3 数组w from X使得每一行w从整个数组中
  • XmlAdapter 到 JAXB 绑定 Joda 的时间间隔?

    我已经被 Web 服务的 JAXB 绑定问题困扰了几个小时 为了准备一个必须返回 Joda Time 类实例 即时 持续时间 间隔等 的更大的 Web 服务 我从一个只有一个返回 Interval 的方法的 Web 服务开始 package
  • Xamarin Forms ListView onpressing 事件

    当按下项目时 我想通过绑定 以编程方式 更改 IconToShow 属性来更改图像 如何触发 OnPressed 事件
  • 是否可以使用不在 GROUP BY 中的 ORDER BY 列?

    正如标题所说 这是我的代码 SELECT material SUM Amount AS Amount RIGHT CONVERT varchar 50 date in 106 8 FROM rec stats GROUP BY materi
  • 适用于 Windows 7 的 32 位版本 VS Code

    我正在尝试获取可在 32 位 Windows 7 上运行的 VS Code 版本 这一页 https code visualstudio com docs supporting FAQ can i run vs code on window
  • 最大化列表视图中的可见行

    这是我的列表中项目的代码 假设我正在夸大这个TextView进入ListView
  • 默认情况下隐藏 JupyterLab 单元的输出

    我在用Jupyter实验室构建使用 bash 和 python 脚本的生物信息学管道 第一个 bash 脚本结果为该过程的每一步提供了大量反馈 但是 此反馈没有帮助 除非出现错误 并且使文档的可读性较差 我希望能够默认隐藏该单元格的输出 而
  • Android 应用安装验证

    我有一个应用程序 其中列出了用户可以安装并赚取积分的一些活动 应用程序列表 现在我主要关心的是安全性 一些用户从模拟器或VPN或其他东西安装应用程序 这样我的客户就无法在Google Play商店中安装应用程序 我见过一些应用程序 如现金海
  • 结果身份改变

    我正在使用 TOR 我想知道如何在需要国家 地区的结果节点之间切换 我可以简单地通过 telnet 9051 端口来更改它 例如 telnet localhost 9051 AUTHENTICATE r signal NEWNYM r qu
  • UITableViewCell 中的自定义 VoiceOver 操作

    When a UITableView是可编辑的 其UITableViewCells允许用户在 VoiceOver 打开时执行自定义操作 当 VoiceOver 光标位于单元格上时 用户可以通过向上或向下滑动来听到可用的操作 然后通过双击屏幕
  • Django 抛出此错误:SMTPException:服务器不支持 STARTTLS 扩展

    由于 gmail 中发送邮件的限制 我在我的一台服务器上安装了 exim4 设置如下 dc eximconfig configtype internet dc other hostnames mydomain com localhost l
  • 向其他用户授予对 v$session 的 SELECT 访问权限

    我想将 v session 的 SELECT 访问权限授予其他用户Oracle Database 11g Enterprise Edition Release 11 2 0 1 0 64bit Production 但是当我运行这个查询时
  • Flutter 无效参数:超出最大调用堆栈大小

    我的 Flutter 应用程序出现了一个我不理解的异常 这是代码 主要 dart void main runApp MyApp class MyApp extends StatelessWidget This widget is the r
  • 检查消息是否是 DM。 (Discord.js 和 Discord.js-commando)

    如何在 Discord js 中检查消息是否为私信 我尝试了几种方法来测试这一点 我尝试过以下方法 if msg channel isDM Produced undefined if msg isDM Produced undefined
  • 我如何在 Android 中跟踪收到的短信?

    我正在开发一个应用程序 想要跟踪传入的短信 我需要一个可以使用的示例代码或例程 如果您为传入短信实施广播接收器 在这种情况下 以下代码将跟踪您传入的短信并为您提供消息和发件人号码 import android content Broadca
  • Swift 3 中的隐藏按钮

    我刚刚开始编码 我真的很想知道如何隐藏按钮 我想做的是当我按下按钮时 Start 我想让开始按钮和后退按钮消失 这是通过插座和操作完成的 我已经搜索了一下 但是当我想做的时候 它说do关键字应该指定一个语句块 我希望有人能帮助我 连接插座后
  • 如何使构造函数只能由基类访问?

    如果我想要一个只能从子类访问的构造函数 我可以使用protected构造函数中的关键字 现在我想要相反的 我的子类应该有一个构造函数 该构造函数可以由其基类访问 但不能从任何其他类访问 这可能吗 这是我当前的代码 问题是子类有一个公共构造函
  • Symfony 4 参数没有类型提示,您应该显式配置其值

    交响乐4 2 3 最近从版本 3 4 升级到 4 2 3 并使我的项目正常运行 但是 当将 services yaml 中的 autoconfigure 设置为 true 时 我将收到以下错误消息 Cannot autowire servi