gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:18:49 24 4
gpt4 key购买 nike

在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com