“变量不存在”FOSuserBundle覆盖login_content模板

时间:2017-10-21 18:46:17

标签: php symfony templates twig fosuserbundle

我想覆盖FOSuser login_content模板,但Symfony会返回此错误:Screen of the error

Twig无法找到变量错误。所以我将{%if error%}替换为{%if error is defined%},就像这个答案告诉我做的那样:Variable "error" does not exist in FOSUserBundle::layout.html.twig at line 5
它删除了变量“error”的错误,但现在它返回与变量“csrf_token”完全相同的错误。 我想有一个比在任何地方添加“已定义”更好的解决方案。

现在我只是在app / Resources中的FOSuserBundle / views中创建文件,其中包含我想要覆盖的文件的确切名称。

当我退出并且我尝试访问作为受限访问的页面时发生错误。如果我尝试进入登录页面,我确实有登录表单,我可以登录。
它看起来像路线错误,但我找不到

这是我的代码:

{# app\Resources\FOSUserBundle\views\Security\login_content.html.twig #}

{% trans_default_domain 'FOSUserBundle' %}

{% if error %}
    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}

<form action="{{ path("fos_user_security_check") }}" method="post">
    {% if csrf_token %}
        <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
    {% endif %}
    <div class="form-group">
        <label for="username">{{ 'security.login.username'|trans }}</label>
        <input type="text" id="username" name="_username" value="{{ last_username }}" required="required" class="form-control"/>
    </div>
    <div class="form-group">
        <label for="password">{{ 'security.login.password'|trans }}</label>
        <input type="password" id="password" name="_password" required="required" class="form-control"/>
    </div>

    <div class="checkbox">
        <label for="remember_me">
            <input type="checkbox" id="remember_me" name="_remember_me" value="on" />
            {{ 'security.login.remember_me'|trans }}
        </label>
    </div>
    <input  type="submit"
            class ="btn btn-success"
            id="_submit"
            name="_submit" 
            value="{{ 'security.login.submit'|trans }}" />
</form>

此代码在此处调用:

{# app\Resources\FOSUserBundle\views\Security\login.html.twig #}

{% extends "@FOSUser/layout.html.twig" %}

{% block fos_user_content %}
    {{ include('@FOSUser/Security/login_content.html.twig') }}
{% endblock fos_user_content %}

扩展:

{# app\Resources\FOSUserBundle\views\layout.html.twig #}

{% extends '@OCCore/layout.html.twig' %}

{% block content %}
    <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>
{% endblock %}

扩展:

{# src/OC/CoreBundle/Resources/views/layout.html.twig #}

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>{% block title %}Auréole{% endblock %}</title>

    {% block stylesheets %}
        {# On charge le CSS de bootstrap depuis le site directement #}
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    {% endblock stylesheets %}
  </head>
    <body>
        {% include 'top-navbar.html.twig' %} {#cherche la navbar \app\Resources\views\navbar.html.twig #}

        <div class="container">
            <div id="header" class="jumbotron">
              <h1>Auréole</h1>
              <p>
                Auréole est un projet d'Enactus Centralesupelec
              </p>
              <p>
                <a class="btn btn-primary btn-lg" href="https://www.facebook.com/EnactusCS/">
                  Retrouvez nous sur Facebook
                </a>
              </p>
            </div>
            <div class="row">
                <div id="menu" class="col-md-3">
                    <h3>Les annonces</h3>
                    <ul class="nav nav-pills nav-stacked">
                      <li><a href="{{ path('oc_platform_home') }}">Accueil</a></li>
                      {% if is_granted('ROLE_AUTEUR') %}
                        <li><a href="{{ path('oc_platform_add') }}">Ajouter une annonce</a></li>
                      {% endif %}
                    </ul>

                </div>
                <div id="content" class="col-md-9">
                    {% block body %}
                    {% endblock %}
                </div>
            </div>

            <hr>

            <footer>
                <p>Born to be Wild © {{ 'now'|date('Y') }}.</p>
            </footer>
        </div>

        {% block javascripts %}
            {# Ajoutez ces lignes JavaScript si vous comptez vous servir des fonctionnalités du bootstrap Twitter #}
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
            <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
        {% endblock %}
    </body>
</html>

0 个答案:

没有答案