2012-06-15 146 views
7

我只是想嘗試已經幾個小時來找出註銷操作後如何讓Flash消息工作。Symfony註銷處理程序

security.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 

config.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); 
    } 
} 

v IEW你好登錄

{% 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> 

後的問題是,它似乎重定向期間閃光消息都被丟棄。 有什麼辦法可以做到這一點?

感謝您的答案。

+0

你能向我們展示視圖嗎? – sensorario

回答

19

會話在註銷時被破壞。但是您可以通過在security.yml文件中添加invalidate_session: false來更改此行爲。

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

查看reference documentation瞭解更多信息。

如果您仍想要使會話無效,可以直接在您的請求中設置一個標誌並將其與內核偵聽器一起使用。

+1

如果使用** invalidate_session ** Flash出現,但RedirectResponse未發生。 – cavvako

+0

我會建議考慮symfony 2.1版本 - 它已經修改了閃存消息以及其他兼容性中斷。 –