本文介绍了Symfony2 重定向事件侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个内核事件侦听器设置 (kernel.controller) 以在用户未登录时重定向该用户.事件侦听器已成功调用,但是我无法弄清楚如何重定向.这是我得到的:
I have a kernel event listener setup (kernel.controller) to redirect the user if he's not logged in. The event listener is succesfully getting called, however I'm having trouble figuring out how to redirect. Here's what I got:
$cont = $event->getController('testpost');
$event->setResponse($cont);
这给了我错误:
Fatal error: Call to undefined method SymfonyComponentHttpKernelEventFilterControllerEvent::setResponse()
推荐答案
您不能设置对 FilterControllerEvent
对象的响应.你需要监听 kernel.request
事件,因为它的 GetResponseEvent
对象有 setResponse()
方法:
You can't set a response to a FilterControllerEvent
object. You need to listen for the kernel.request
event because its GetResponseEvent
object has the setResponse()
method:
$route = 'route_name';
if ($route === $event->getRequest()->get('_route')) {
return;
}
$url = $this->router->generate($route);
$response = new RedirectResponse($url);
$event->setResponse($response);
这篇关于Symfony2 重定向事件侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!