从继承覆盖FOSUserBundle的模板 [英] Overriding FOSUserBundle's templates from inheritance

查看:129
本文介绍了从继承覆盖FOSUserBundle的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我正在尝试使用自己的模板自定义FOSUserBundle的模板。但它根本不起作用。我尝试了我发现的每个帖子中所说的一切,校对了所有内容,清除了缓存数千次,仍然无法正常工作。



使用getParent捆绑类:

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

namespace PLL\ UserBundle;

使用Symfony \Component \HttpKernel \ Bundle \ Bundle;

类PLLUserBundle扩展Bundle
{
公共函数getParent()
{
返回'FOSUserBundle';
}
}
?>

应该覆盖fos'的模板:

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

{%extendsPLLCoreBundle :: layout.html .twig%}

{%block body%}

{%为密钥,app.session.flashbag.all()中的消息%}
{邮件中邮件的百分比%}
< 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()%}
{%in 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,或者手动删除项目根目录中的var文件夹。甚至删除了我浏览器的缓存。



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



如果它们可以提供任何帮助,



我的security.yml文件:

  // app / config / security.yml 

安全:
[...]
防火墙:
main:
模式:^ /
anonymous:true
提供者:main
form_login:
login_path:fos_user_security_login
check_path:fos_user_security_check
logout:
路径:fos_user_security_logout
目标:/ platform
[...]

应用程序的路由文件:

  // app / config / routing.yml 

[...]

fos_user_security:
资源:@ FOSUserBundle / Resources / config /routing/security.xml

fos_user_profile:
资源:@ FOSUserBundle / Resources / config / routing / profile.xml
pref ix:/ profile

fos_user_register:
资源:@ FOSUserBundle / Resources / config / routing / registration.xml
前缀:/ register

fos_user_resetting:
资源:@ FOSUserBundle / Resources / config / routing / resetting.xml
前缀:/ resetting

fos_user_change_password:
资源:@ FOSUserBundle / Resources / config / routing / change_password.xml
前缀:/ profile

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

解决方案

如果我理解正确,你需要覆盖FOSUserBundle的模板,比如用于登录的模板,或者注册的那个,依此类推。



为了做到这一点,你需要手动复制来自供应商的所有目录/文件/ friendsofsymfony / user_bundle / Resources / views into app / Resources / FOSUserBundle / views



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


As the title suggests I'm trying to customize FOSUserBundle's templates with my own ones. But it doesn't work at all. I tried everything as stated in every posts I found, proofread everything, cleared the cache thousands of times, still not working.

Bundle class with getParent :

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

namespace PLL\UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

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

The template that should override the fos' one :

{# 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' template, the one that is actually being used by 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>

Going to /web/app_dev.php/login still shows the FOS' layout, even after deleting the cache over and over again. Either with a php bin/console cache:clear, or deleting "by hand" the var folder in my project's root. Even deleted my browser's cache.

Not to mention, extending "PLLCoreBundle::layout.html.twig" works perfectly fine in any other views on my website. So I don't think it comes from an error in my twig template.

In case they can be of any help,

My security.yml file :

// 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
[...]

The app's routing file :

// 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

So... What am I missing ?

解决方案

If I understood correctly, you need to override FOSUserBundle's templates, like the one for login, or the one for register, and so on.

In order to do that, you need to copy, manually, all the dirs/files from vendor/friendsofsymfony/user_bundle/Resources/views into app/Resources/FOSUserBundle/views.

And then, if you want to modify the template for the login page, then you can can put your own template in the app/Resources/FOSUserBundle/views/Security/login_content.html.twig.

这篇关于从继承覆盖FOSUserBundle的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆