Overriding FOSUserBundle's templates from inhe

2019-02-16 00:37发布

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 ?

1条回答
我只想做你的唯一
2楼-- · 2019-02-16 00:58

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.

查看更多
登录 后发表回答