Symfony,在控制器中强制注销
我正在使用Symfony 3.4,我想在控制器中的操作结束时注销我的用户 这就是行动Symfony,在控制器中强制注销,symfony,symfony-security,Symfony,Symfony Security,我正在使用Symfony 3.4,我想在控制器中的操作结束时注销我的用户 这就是行动 public function changeUserEmail() { /* change the user email */ /* perform the logout */ /* choose the route to redirect to */ return $this->redirectToRoute(/* some route choosen above
public function changeUserEmail() {
/* change the user email */
/* perform the logout */
/* choose the route to redirect to */
return $this->redirectToRoute(/* some route choosen above */);
}
是否有一种方法可以实现/*以Symfony方式执行注销*/
?我在文件里什么也没找到。
我确实想在控制器中注销(不想重定向到注销路径),并且我想在控制器中选择要重定向的路由
非常感谢
版本或符号是3.4答案如下
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
// ...
public function changeUserEmail(TokenStorageInterface $tokenStorage) {
/* change the user email */
$tokenStorage->setToken();
/* choose the route to redirect to */
return $this->redirectToRoute(/* some route choosen above */);
}
无需使所有会话无效,例如,如果一个会话定义了多个防火墙。我想您只需将安全上下文令牌设置为null,然后使会话无效即可。在使用PHP时更像是一个Laravel的家伙,但理论上这也应该适用于Symfony 4。不确定这是否应该存在于控制器中。一些随机控制器将令牌存储设置为空似乎有点不合适?为什么不直接重定向到应用程序的注销处理程序?@James,因为我不相信用户会遵循重定向,因为我确实想选择控制器内部的路由。是的,控制器应该决定发送到的最终路由。但是,设置令牌属于auth或security层,这是一个新类,可以在不同的位置调用它来设置(或清空)用户的令牌,或者使用应用程序的注销处理程序。如果您有随机问题/错误,例如注销或其他,搜索控制器以查看用户可能已注销的位置是令人厌烦的。