简体   繁体   English

Symfony 为注销路由添加逻辑

[英]Symfony add logic to logout route

In a Symfony 5.0 Application I want to add custom logic for cleanup reasons when the user loggs out.在 Symfony 5.0 应用程序中,我想在用户注销时出于清理原因添加自定义逻辑。 What I have currenty is what is described in the docs:我目前拥有的是文档中描述的内容:

https://symfony.com/doc/current/security.html#logging-out https://symfony.com/doc/current/security.html#logging-out

As the logout() function in the SecurityController is intercepted by Symfony it won't work to add logic there.由于 SecurityController 中的 logout() 函数被 Symfony 拦截,因此无法在那里添加逻辑。

So - where CAN I add logic which is allways executed when a user loggs out?那么 - 我可以在哪里添加在用户注销时始终执行的逻辑? Couldn't find anything in the docs so far...到目前为止在文档中找不到任何内容...

IMHO the best option is to implement the LogoutHandlerInterface referenced slightly indirectly on the symfony docs you mentioned (it mentions the LogoutSuccessHandlerInterface , which has a comment regarding the LogoutHandlerInterface : "If you want to only perform some logout related clean-up task, use the LogoutHandlerInterface instead.")恕我直言,最好的选择是在你提到的symfony 文档中稍微间接地实现LogoutHandlerInterface (它提到了LogoutSuccessHandlerInterface ,它有关于LogoutHandlerInterface的评论:“如果你只想执行一些与注销相关的清理任务,请使用 LogoutHandlerInterface反而。”)

To me, it feels like an event handler, but it doesn't use the event system, so ... whatever.对我来说,它感觉就像一个事件处理程序,但它不使用事件系统,所以......无论如何。

Nevertheless, the arguments provided to your LogoutHandler will receive a TokenInterface which has TokenInterface::getUser() which returns the current user.尽管如此,提供给您的LogoutHandler的参数将接收一个TokenInterface ,它具有返回当前用户的TokenInterface::getUser() (dependency injection on the constructor should provide you with additional services and stuff) (对构造函数的依赖注入应该为您提供额外的服务和东西)

The alternative would be to implement an EventListener for the kernel request event - which the LogoutListener of the symfony/security package uses to manage logouts.另一种方法是为内核请求事件实现一个 EventListener - symfony/security包的LogoutListener使用它来管理注销。 You would have to add the same logic as the LogoutListener and probably have to make it trigger before the LogoutListener , this is however a slightly more convoluted way and I would really use the LogoutHandler .您必须添加与LogoutListener相同的逻辑,并且可能必须在LogoutListener之前触发它,但是这是一种稍微复杂的方法,我真的会使用LogoutHandler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM