从FilterControllerEvent侦听器重定向到另一个Symfony路由 [英] Redirect to Another Symfony Route from FilterControllerEvent Listener

查看:122
本文介绍了从FilterControllerEvent侦听器重定向到另一个Symfony路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个kernal.controller侦听器,以在函数返回true时重定向到另一条路由.我有可用的路由,但是无法使用$event->setController()从该路由设置控制器.

I am trying to set up a kernal.controller listener to redirect to another route when a function returns true. I have the route available to me but no way to set the controller from this route using $event->setController().

我遇到以下错误:

FilterControllerEvent.php第59行中的

FatalThrowableError: 类型错误:传递给Symfony \ Component \ HttpKernel \ Event \ FilterControllerEvent :: setController()的参数1必须是可调用的,给出的字符串为

FatalThrowableError in FilterControllerEvent.php line 59: Type error: Argument 1 passed to Symfony\Component\HttpKernel\Event\FilterControllerEvent::setController() must be callable, string given

有人对我如何完成此工作有建议吗?

Does anyone have suggestions on how I can complete this?

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函数使其正常运行.感谢您的帮助!

We were able to get it working by using a Lambda function. Thanks for the help!

    if ($block instanceof Block) {
        $redirectUrl = $this->router->generate($block->getRoute());
        $event->setController(function() use ($redirectUrl) {
            return new RedirectResponse($redirectUrl);
        });
    };

这篇关于从FilterControllerEvent侦听器重定向到另一个Symfony路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆