php - Symfony 4 FosUserBundle Controller 装饰

标签 php symfony fosuserbundle symfony4

我正在使用 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 中使用它。

我应该注入(inject)fos_user.template.engine还是有另一种语法可以将其放入方法中?

最佳答案

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

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

如果您检查 SecurityControllerrenderLogin() 方法,你正在装饰的类,你会发现它是这样的:

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

关于php - Symfony 4 FosUserBundle Controller 装饰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54826825/

相关文章:

php - Magento数据库升级1.4.2到1.9验证

php - Symfony2 记住我不适用于 fos 用户包

php - 帮助我在 Mysql 中创建巨大的数据库

PHP 绑定(bind)变量、无效参数号错误或 PDO 更新语句

javascript - 如何通过 jQuery Ajax 发布数据在 PHP 中对 JSON 进行编码?

symfony - 尝试登录时陷入循环

symfony - 如何从 PHP 预填充 EntityType 类型的字段

javascript - 如何在 Symfony2 中使用 JavaScript 为其他页面生成 URL?

email - 如何在 FOSUserBundle 中为 Controller 重置设置 sender_name?

php - Symfony 4.3.1 和 FosUserBundle 2.1.2 的问题