2012-06-15 29 views
7

Tôi chỉ đang cố gắng vài giờ để tìm ra cách để nhận được thông báo flash hoạt động sau khi hành động đăng xuất.Trình xử lý đăng xuất 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 chào sau khi đăng nhập

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

xem mẫu đăng nhập

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

bố trí mẫu chính

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

Vấn đề là có vẻ như tin nhắn nhấp nháy là nhận được giảm xuống trong chuyển hướng. Có cách nào để thực hiện việc này không?

Cảm ơn câu trả lời.

+0

Bạn có thể chỉ cho chúng tôi chế độ xem không? – sensorario

Trả lời

19

Phiên bị hủy khi đăng xuất. Nhưng bạn có thể thay đổi hành vi này bằng cách thêm invalidate_session: false vào tệp security.yml của mình.

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

Kiểm tra reference documentation để biết thêm thông tin.

Nếu bạn vẫn muốn vô hiệu phiên, bạn có thể đặt cờ trong Yêu cầu trực tiếp và sử dụng nó với trình nghe Kernel.

+1

trong trường hợp sử dụng ** invalidate_session ** Flash xuất hiện nhưng RedirectResponse không xảy ra. – cavvako

+0

Tôi khuyên bạn nên xem xét phiên bản symfony 2.1 - nó đã sửa lại các thông báo flash cũng như các khả năng tương thích khác cho tốt hơn. –

Các vấn đề liên quan