Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2从事件侦听器重定向到Url_Php_Symfony - Fatal编程技术网

Php Symfony2从事件侦听器重定向到Url

Php Symfony2从事件侦听器重定向到Url,php,symfony,Php,Symfony,当symfony抛出异常时,我需要从EventListener重定向到自定义url 为我效劳 app.exception_listener: class: AppBundle\EventListener\ExceptionListener tags: - { name: kernel.event_listener, event: kernel.exception, method: onKernelException } 在我的php课上 <?php n

当symfony抛出异常时,我需要从EventListener重定向到自定义url

为我效劳

app.exception_listener:
    class: AppBundle\EventListener\ExceptionListener
    tags:
        - { name: kernel.event_listener, event: kernel.exception, method: onKernelException  }
在我的php课上

<?php

namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\RedirectResponse;

class ExceptionListener {

public function onKernelException(GetResponseForExceptionEvent $event) {
    // You get the exception object from the received event
    $exception = $event->getException();

    if ($exception instanceof NotFoundHttpException) {

        //i need this function
        return $this->redirect('https://www.google.co.ve/?gws_rd=ssl', 404);
    }
}

}
而不是:

返回$this->redirect(“”,404)

做:

$event->setResponse(新建) 重定向响应(“”)

还要注意的是,用404响应发送位置头并没有实际意义,您应该使用3xx范围内的响应。Symfony在尝试使用其他状态代码重定向时,实际上(正确地)会抛出异常。

而不是:

返回$this->redirect(“”,404)

做:

$event->setResponse(新建) 重定向响应(“”)

还要注意的是,用404响应发送位置头并没有实际意义,您应该使用3xx范围内的响应。Symfony在尝试使用另一个状态代码重定向时,实际上(正确地)会抛出一个异常