service - Symfony 4 参数没有类型提示,你应该明确配置它的值
问题描述
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.
我的服务.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
或者您可以使用本地服务绑定功能将参数绑定到标量参数:
services:
_defaults:
bind:
$localeActive: '%locale_active%'
文档: https ://symfony.com/blog/new-in-symfony-3-4-local-service-binding
推荐阅读
- sql - SQL Server CE 格式查询
- php - 从自定义 url 链接将购物车商品数据添加到 woocommerce
- c++ - 移动鼠标时 QCursor::setPos() 不起作用
- swift - 使用 Swift 在 macOS 的 NSStatusBar 上显示图标和文本
- php - Phpinfo() 在 NginX 更新后显示旧 PHP 版本
- javascript - 为什么我的 nodeJS 中的 console.log 没有给出正确的结果?
- r - R:如何使用 geom_point 将点添加到并排错误栏
- nginx - 如何在没有 RedHat 模块的情况下安装 nginx
- java - KeyStore API 抛出错误:java.security.KeyStoreException: BCFIPS JKS 存储是只读的,仅支持证书条目
- python - 与线程共享的内存