将Symfony2 LogoutSuccessHandler重定向到原始注销目标 [英] Redirect Symfony2 LogoutSuccessHandler to original logout target
问题描述
我需要在注销时修改我的用户对象.为此,我有一个security.yml,其中包含以下内容(以及其他内容)-
I need to modify my user object on logout. To do this, I have a security.yml that contains the following (amongst other things) -
#...
logout:
success_handler: my.logout_success_handler
target: /
#...
...这定义了注销成功处理程序,它在services.yml中是这样定义的-
...this defines a logout success handler, which is defined in services.yml like this -
my.security.logout_success_handler:
class: My\Security\LogoutSuccessHandler
arguments: ["@security.context", "@doctrine.orm.default_entity_manager"]
...最后,我的处理程序的业务端就是这样-
...finally, the business-end of my handler is like this -
// ...
public function onLogoutSuccess(Request $request)
{
$user = $this->securityContext->getToken()->getUser();
// ... do stuff with the user object....
$this->em->flush();
// now what?
}
// ...
所以,它说现在是什么?"我了解我需要返回一个Response对象.理想情况下,我希望该响应对象将用户重定向到security.yml中logout.target中定义的任何内容.
So, where it says "now what?" I understand that I need to return a Response object. Ideally I want that response object to redirect the user to whatever is defined in logout.target in the security.yml.
请问有一种简单的方法可以查询吗?或者,甚至更好的是,还有另一种不需要我完全参与请求/响应对象的事情吗?
Is there an easy way I can query that? Or, even better, is there another way of doing this kind of thing that doesn't require me to get involved with the request/response objects at all?
谢谢
推荐答案
所以,我认为我已经找到了正确的答案-
So, I think I've figured out the right answer -
我的security.yml而不是实现LogoutSuccessHandlerInterface
和配置logout.success_handler
,现在看起来像这样-
Rather than implementing LogoutSuccessHandlerInterface
and configuring a logout.success_handler
, my security.yml now looks like this -
# ...
logout:
handlers: [my.bundle.security.logout_handler]
# ...
...并且我正在实现Symfony\Component\Security\Http\Logout\LogoutHandlerInterface
.令人困惑的命名,但是这似乎是注销后操作的首选方式,而不必参与响应对象.我的实现看起来像这样-
...and I'm implementing Symfony\Component\Security\Http\Logout\LogoutHandlerInterface
. Confusing naming, but this seems to be the preferred way of doing post-logout operations without having to get involved with the response object. My implementation looks like this -
namespace My\Bundle\Security;
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;
/**
* Do post logout stuff
*/
class LogoutHandler implements LogoutHandlerInterface
{
/**
* @var EntityManager
*/
protected $em;
/**
* Constructor
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* Do post logout stuff
*/
public function logout(Request $request, Response $response, TokenInterface $authToken)
{
$user = $authToken->getUser();
// do stuff with the user object...
$this->em->flush();
return $response;
}
}
...如您所见,LogoutHandlerInterface
提供了一个预制的$response
对象,我可以在完成后立即将其返回.
...as you can see, the LogoutHandlerInterface
provides a pre-made $response
object that I can just return when I'm finished.
这篇关于将Symfony2 LogoutSuccessHandler重定向到原始注销目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!