Symfony 4 FosUserBundle控制器装饰

问题描述 投票:2回答:1

我正在使用FosUserBundle装饰基类SecurityController:

namespace App\Controller;

use FOS\UserBundle\Controller\SecurityController as BaseController;

class SecurityController extends BaseController
{
   public function renderLogin(array $data)
   {

    $template = sprintf('FOSUserBundle:Security:login.html.%s', $this->container->getParameter('fos_user.template.engine'));

. . .

在我的Services.yaml中:

App\Controller\SecurityController:
    decorates: fos_user.security.controller

问题是我收到错误:

The parameter "fos_user.template.engine" must be defined.

与renderLogin方法中的行相关:

$template = sprintf('FOSUserBundle:Security:login.html.%s', $this->container->getParameter('fos_user.template.engine'));

我怎样才能得到这个缺失的参数?以上行来自Symfony2,现在我在Symfony4中使用它。

我应该注入fos_user.template.engine还是有另一种语法来让它进入方法?

php symfony fosuserbundle symfony4
1个回答
2
投票

特定错误意味着未定义fos_user.template.engine参数。

此参数是在旧版本的FOS_User上定义的,但不再需要,并且不需要。您可以在配置文件中将其定义为twig,但最好使用当前FOS_User使用的方法。

如果你检查renderLogin()SecurityController方法,你正在装修的课程,你会发现它是这样的:

protected function renderLogin(array $data)
{
    return $this->render('@FOSUser/Security/login.html.twig', $data);
}

根据你的扩展方法,你可以完全摆脱$template = sprintf() ...线。

例如:

protected function renderLogin(array $data) {
    $data['foo'] = "bar";
    // do whatever else you need to do

    return parent::renderLogin($data);
}
© www.soinside.com 2019 - 2024. All rights reserved.