本文介绍了Symfony:注销后如何显示成功消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Symfony中,用户成功注销后,如何显示诸如您已成功注销"之类的成功消息?
In Symfony, after a user successfully log out, how to display a success message like "you have successfully logged out" ?
推荐答案
1)创建一个新服务来处理注销成功事件.
1) Create a new service to handle the logout success event.
在services.yml
中添加服务:
logout_success_handler:
class: Path\To\YourBundle\Services\LogoutSuccessHandler
arguments: ['@security.http_utils']
并添加类,将/path/to/your/login
替换为登录页面的网址(在控制器的最后一行):
And add the class, replacing /path/to/your/login
with the url of your login page (in the last line of the controller):
<?php
namespace Path\To\YourBundle\Services;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
class LogoutSuccessHandler implements LogoutSuccessHandlerInterface
{
protected $httpUtils;
protected $targetUrl;
/**
* @param HttpUtils $httpUtils
*/
public function __construct(HttpUtils $httpUtils)
{
$this->httpUtils = $httpUtils;
$this->targetUrl = '/path/to/your/login?logout=success';
}
/**
* {@inheritdoc}
*/
public function onLogoutSuccess(Request $request)
{
$response = $this->httpUtils->createRedirectResponse($request, $this->targetUrl);
return $response;
}
}
2)配置您的security.yml
以使用刚创建的自定义LogoutSuccessHandler
:
2) Configure your security.yml
to use the custom LogoutSuccessHandler
just created:
firewalls:
# ...
your_firewall:
# ...
logout:
# ...
success_handler: logout_success_handler
3)在登录页面的树枝模板中添加:
3) In the twig template of your login page add:
{% if app.request.get('logout') == "success" %}
<p>You have successfully logged out!</p>
{% endif %}
这篇关于Symfony:注销后如何显示成功消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!