gpt4 book ai didi

service - Symfony 4 参数没有类型提示,你应该明确配置它的值

转载 作者:行者123 更新时间:2023-12-03 16:56:09 24 4
gpt4 key购买 nike

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

我试过的:
  • 在 RedirectToLocaleActiveListener
  • 的 __construct 中将“字符串”添加到 $localActive

    结果:
    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

    或者您可以使用本地服务绑定(bind)功能将参数绑定(bind)到标量参数:
    services:
    _defaults:
    bind:
    $localeActive: '%locale_active%'

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

    关于service - Symfony 4 参数没有类型提示,你应该明确配置它的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54825231/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com