![](/img/trans.png)
[英]Woocommerce : how to display custom success notice message on Logout?
[英]Symfony: how to display a success message after logout
在Symfony中,用户成功注销后,如何显示诸如“您已成功注销”之类的成功消息?
1)创建一个新服务来处理注销成功事件。
在services.yml
添加服务:
logout_success_handler:
class: Path\To\YourBundle\Services\LogoutSuccessHandler
arguments: ['@security.http_utils']
并添加类,将/path/to/your/login
替换为登录页面的网址(在控制器的最后一行):
<?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)在登录页面的树枝模板中添加:
{% if app.request.get('logout') == "success" %}
<p>You have successfully logged out!</p>
{% endif %}
恕我直言,有一种绝对简单的方法。 在security.yaml
,使用target
密钥定义注销后重定向到的路由:
security:
firewalls:
main:
[...]
logout:
path: /logout
target: /logout_message
然后在控制器中( SecurityController.php
适合此操作),定义此操作,仅添加Flash消息,然后重定向到所需位置(此示例中为home):
/**
* @Route("/logout_message", name="logout_message")
*/
public function logoutMessage()
{
$this->addFlash('success', "You've been disconnected. Bye bye !");
return $this->redirectToRoute('home');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.