我正在尝试设置一个 kernel.controller 侦听器,以便在函数返回 true 时重定向到另一个路由。我有可用的路线,但无法使用此路线设置控制器$event->setController()
.
我收到以下错误:
FilterControllerEvent.php 第 59 行中的 FatalThrowableError:
类型错误:传递给 Symfony\Component\HttpKernel\Event\FilterControllerEvent::setController() 的参数 1 必须可调用,给出字符串
有人对我如何完成这个有建议吗?
class BlockListener
{
public function onKernelController(FilterControllerEvent $event)
{
$block = $this->blockService->checkForBlock($user->getId());
if ($block instanceof Block) {
// $block-getRoute() is a standard Symfony route string. It doesn't work!
$event->setController($block->getRoute());
}
}
}
我们可以通过使用 Lambda 函数来使其工作。谢谢您的帮助!
if ($block instanceof Block) {
$redirectUrl = $this->router->generate($block->getRoute());
$event->setController(function() use ($redirectUrl) {
return new RedirectResponse($redirectUrl);
});
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)