从FilterControllerEvent侦听器

时间:2016-11-05 00:10:04

标签: php symfony redirect listener

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

我收到以下错误:

  

FilterControllerEvent.php第59行中的FatalThrowableError:   类型错误:参数1传递给Symfony \ Component \ HttpKernel \ Event \ FilterControllerEvent :: setController()必须是可调用的,给定字符串

有没有人就如何完成此事提出建议?

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());
        }
    }
}

3 个答案:

答案 0 :(得分:2)

我们能够通过使用Lambda函数使其工作。谢谢你的帮助!

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

答案 1 :(得分:1)

您可以根据需要修改以下选项。

选项1

Full details

LISTENER

namespace Application\BackendBundle\EventListener;

use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class KernelExceptionListener
{
    private $router;
    private $redirectRouter = 'application_frontend_default_index';

    public function __construct(Router $router)
    {
        $this->router = $router;
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();

        if ($exception instanceof NotFoundHttpException) {
            if ($event->getRequest()->get('_route') == $this->redirectRouter) {
                return;
            }

            $url = $this->router->generate($this->redirectRouter);
            $response = new RedirectResponse($url);
            $event->setResponse($response);
        }
    }
}

服务定义

services:
    application_backend.event_listener.kernel_exception:
        class: Application\BackendBundle\EventListener\KernelExceptionListener
        arguments: [@router]
        tags:
            - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

选项2

Full details

LISTENER

namespace Application\FrontendBundle\Listener;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

class PlayerListener
{
    public function onKernelController(FilterControllerEvent $event)
    {
        $message = 'Bye inanzzz';

        $event->setController(
            function() use ($message) {
                return new Response($message, 400);
            }
        );
    }
}

服务定义

services:
    application_frontend.listener.player:
        class: Application\FrontendBundle\Listener\PlayerListener
        tags:
            - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }

答案 2 :(得分:0)

请改为尝试:

$event->setController( $event->getController() );

我认为它应该有用,但不能保证。 这会传入控制器而不是字符串,这就是您的错误所指示的内容。