从继承中覆盖 FOSUserBundle 的模板

Overriding FOSUserBundle's templates from inheritance

如标题所示,我正在尝试用我自己的模板自定义 FOSUserBundle 的模板。但它根本不起作用。我按照我找到的每篇文章中的所有内容进行了尝试,校对了所有内容,清除了数千次缓存,仍然无法正常工作。

使用 getParent 捆绑 class :

<?php
// src/PLL/UserBundle/PLLUserBundle.php

namespace PLL\UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class PLLUserBundle extends Bundle
{
  public function getParent()
  {
    return 'FOSUserBundle';
  }
}
?>

应该覆盖 fos 的模板:

{# src/PLL/UserBundle/Resources/views/layout.html.twig #}

{% extends "PLLCoreBundle::layout.html.twig" %}

{% block body %}

  {% for key, messages in app.session.flashbag.all() %}
    {% for message in messages %}
      <div class="alert alert-{{ key }}">
        {{ message|trans({}, 'FOSUserBundle') }}
      </div>
    {% endfor %}
  {% endfor %}

  {% block fos_user_content %}
  {% endblock fos_user_content %}

{% endblock %}

FOS 的模板,Symfony 实际使用的模板:

{# vendor/friendsofsymfony/user-bundle/Resources/views/layout.html.twig #}

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
    </head>
    <body>
        <div>
            {% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
                {{ 'layout.logged_in_as'|trans({'%username%': app.user.username}, 'FOSUserBundle') }} |
                <a href="{{ path('fos_user_security_logout') }}">
                    {{ 'layout.logout'|trans({}, 'FOSUserBundle') }}
                </a>
            {% else %}
                <a href="{{ path('fos_user_security_login') }}">{{ 'layout.login'|trans({}, 'FOSUserBundle') }}</a>
            {% endif %}
        </div>

        {% if app.request.hasPreviousSession %}
            {% for type, messages in app.session.flashbag.all() %}
                {% for message in messages %}
                    <div class="flash-{{ type }}">
                        {{ message }}
                    </div>
                {% endfor %}
            {% endfor %}
        {% endif %}

        <div>
            {% block fos_user_content %}
            {% endblock fos_user_content %}
        </div>
    </body>
</html>

去/web/app_dev.php/login还是显示FOS的布局,即使是一遍又一遍的删除缓存。使用 php bin/console cache:clear,或者删除 "by hand" 我项目根目录中的 var 文件夹。甚至删除了我的浏览器缓存。

更不用说,扩展 "PLLCoreBundle::layout.html.twig" 在我网站上的任何其他视图中都可以正常工作。所以我不认为它来自我的树枝模板中的错误。

如果他们能帮上忙,

我的 security.yml 文件:

// app/config/security.yml

security:
[...]
    firewalls:
        main:
            pattern:   ^/
            anonymous: true
            provider: main
            form_login:
                login_path: fos_user_security_login
                check_path: fos_user_security_check
            logout:
                path:    fos_user_security_logout
                target:  /platform
[...]

应用程序的路由文件:

// app/config/routing.yml

[...]

fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /profile

所以...我错过了什么?

如果我没理解错的话,你需要覆盖FOSUserBundle的模板,比如登录模板,或者注册模板等等。

为此,您需要手动将所有 dirs/files 从 vendor/friendsofsymfony/user_bundle/Resources/views 复制到 app/Resources/FOSUserBundle/views

然后,如果你想修改登录页面的模板,那么你可以把自己的模板放在app/Resources/FOSUserBundle/views/Security/login_content.html.twig