Symfony 注销处理程序 [英] Symfony logout handler
问题描述
我已经尝试了几个小时来弄清楚如何在注销操作后使 Flash 消息正常工作.
security.yml
登录:模式:^/登录$安全:假安全区域:模式:^/表单登录:检查路径:/检查登录路径:/登录failure_handler: authentication_handler登出:路径:/退出成功处理程序:身份验证处理程序
config.yml
服务:身份验证处理程序:类:Project\LoginBundle\Handler\AuthenticationHandler
AuthenticationHandler.php
class AuthenticationHandler 实现了AuthenticationFailureHandlerInterface、LogoutSuccessHandlerInterface{公共函数 onAuthenticationFailure(Request $request, AuthenticationException $exception){$referer = $request->headers->get('referer');$request->getSession()->setFlash('error', $exception->getMessage());返回新的重定向响应($referer);}公共函数 onLogoutSuccess(Request $request){$referer = $request->headers->get('referer');$request->getSession()->setFlash('success', 'Wylogowano');返回新的重定向响应($referer);}}
登录后查看hello
{% extends "ProjectCMSBundle:Secured:layout.html.twig" %}{% 块标题你好"~名字 %}{% 块内容 %}<h1>你好{{姓名}}!</h1><a href="{{ path('_project_secured_hello_admin', { 'name': name }) }}">为<strong>admin</strong>保护的Hello资源仅.{% 结束块 %}{% 设置代码 = 代码(_self)%}
查看登录表单
{% extends 'ProjectCMSBundle::layout.html.twig' %}{% 块标题 %}标题{% 结束块 %}{% 块内容 %}<form action="{{ path("_project_security_check") }}" method="post" id="login"><div class="数据"><div class="用户名"><label for="username"> </label><input type="text" id="username" name="_username" value="{{ last_username }}"/>
<div class="密码"><label for="password"> </label><input type="password" id="password" name="_password"/>
{% 如果错误 %}<div class="error">{{ error.message|trans({},'messages') }}</div>{% 万一 %}<input type="submit" class="submit"/></表单>{% 结束块 %}{% 设置代码 = 代码(_self)%}
布局主模板
<头><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"/>{% 块标题 %}{% endblock %} <link rel="shortcut icon" href="{{ asset('favicon.ico') }}"/>头部><身体><div id="symfony-wrapper">{% if app.session.flash('error') %}<div class="flash-message">{{ app.session.flash('error')|trans }}