8

我只是尝试了几个小时来弄清楚如何在注销操作后让 Flash 消息正常工作。

安全.yml

    login:
        pattern:  ^/login$
        security: false

    secured_area:
        pattern:    ^/
        form_login:
            check_path: /check
            login_path: /login
            failure_handler: authentication_handler
        logout:
            path:   /logout
            success_handler: authentication_handler

配置.yml

services:
    authentication_handler:
        class: Project\LoginBundle\Handler\AuthenticationHandler

AuthenticationHandler.php

class AuthenticationHandler implements AuthenticationFailureHandlerInterface, LogoutSuccessHandlerInterface
{
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {       
        $referer = $request->headers->get('referer');       
        $request->getSession()->setFlash('error', $exception->getMessage());

        return new RedirectResponse($referer);
    }

    public function onLogoutSuccess(Request $request) 
    {
        $referer = $request->headers->get('referer');
        $request->getSession()->setFlash('success', 'Wylogowano');


        return new RedirectResponse($referer);
    }
}

登录后查看你好

{% extends "ProjectCMSBundle:Secured:layout.html.twig" %}

{% block title "Hello " ~ name %}

{% block content %}
    <h1>Hello {{ name }}!</h1>

    <a href="{{ path('_project_secured_hello_admin', { 'name': name }) }}">Hello resource secured for <strong>admin</strong> only.</a>
{% endblock %}

{% set code = code(_self) %}

查看登录表单

{% extends 'ProjectCMSBundle::layout.html.twig' %}

{% block title %}
    Title
{% endblock %}

{% block content %}


    <form action="{{ path("_project_security_check") }}" method="post" id="login">
        <div class="data">
          <div class="username">
              <label for="username">&nbsp;</label>
              <input type="text" id="username" name="_username" value="{{ last_username }}" />
          </div>

          <div class="password">
              <label for="password">&nbsp;</label>
              <input type="password" id="password" name="_password" />
          </div>
        </div>
    {% if error %}
        <div class="error">{{ error.message|trans({},'messages') }}</div>
    {% endif %}
    <input type="submit" class="submit" />
</form>
{% endblock %}

{% set code = code(_self) %}

布局主模板

<!DOCTYPE html>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="{{ asset('bundles/project/css/demo.css') }}" type="text/css" media="all" />
    <title>{% block title %}{% endblock %}</title>
    <link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
</head>
<body>
    <div id="symfony-wrapper">

        {% if app.session.flash('error') %}
            <div class="flash-message">
                {{ app.session.flash('error')|trans }}
            </div>
        {% endif %}

        {% if app.session.flash('success') %}
            <div class="flash-message">
                {{ app.session.flash('success')}}
            </div>
        {% endif %}

        {% if app.user %}
          {% block content_header %}
            <ul id="menu">
                {% block content_header_more %}
                {% endblock %}
            </ul>

            <div style="clear: both"></div>
          {% endblock %}
        {% endif %}

        <div class="symfony-content">
            {% block content %}
            {% endblock %}
        </div>

        {#{% if code is defined %}
            <h2>Code behind this page</h2>
            <div class="symfony-content">{{ code|raw }}</div>
        {% endif %}#}
    </div>
</body>

问题是似乎在重定向期间闪存消息被丢弃。有没有办法做到这一点?

感谢您的回答。

4

1 回答 1

20

会话在注销时被销毁。invalidate_session: false但是您可以通过添加security.yml文件来更改此行为。

logout:
    path:   /logout
    success_handler: authentication_handler
    invalidate_session: false

查看参考文档以获取更多信息。

如果您仍然想使会话无效,您可以直接在您的请求中设置一个标志并将其与内核侦听器一起使用。

于 2012-06-15T12:05:23.997 回答