php - Symfony:注销后如何显示成功消息

标签 php symfony logout

在Symfony中,用户成功登出后,如何显示“您已成功登出”之类的成功信息?

最佳答案

1) 创建一个新的服务来处理注销成功事件。

services.yml添加服务:

logout_success_handler:
    class: Path\To\YourBundle\Services\LogoutSuccessHandler
    arguments: ['@security.http_utils']

并添加类,替换 /path/to/your/login使用登录页面的 url(在 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刚刚创建:
firewalls:
    # ...
    your_firewall:
        # ...
        logout:
            # ...
            success_handler: logout_success_handler

3)在登录页面的 Twig 模板中添加:
{% if app.request.get('logout') == "success" %}
    <p>You have successfully logged out!</p>
{% endif %}

关于php - Symfony:注销后如何显示成功消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41062805/

相关文章:

php - Symfony 3.3 - Autowiring 弃用狂潮

mysql - Symfony 教义 : emojis are not stored

ruby-on-rails - 如何从 Rails 控制台注销设计用户?

具有特定过滤功能的 PHP 搜索表单

php - 使用 PHP 验证表单中的电子邮件输入

javascript - 仅当选中时才将复选框的值传递给变量

ios - 如何从我的应用程序注销并转到初始登录 View ?

php - 避免返回到页面顶部

linux - 从 git 中删除不存在的文件

javascript - AngularJs SPA 注销