Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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
Exception Symfony2-操纵来自内核异常侦听器的请求/响应_Exception_Symfony_Event Listener - Fatal编程技术网

Exception Symfony2-操纵来自内核异常侦听器的请求/响应

Exception Symfony2-操纵来自内核异常侦听器的请求/响应,exception,symfony,event-listener,Exception,Symfony,Event Listener,我正在为一个网站构建一个管理面板,我想更改当404异常发生时调用的视图,但仅适用于管理应用程序(路径:/admin/*) 我已经对网站的error404.html.twig视图(位于app/Resources/TwigBundle/views/Exception/)做了过度修改 我想到了kernel.exception事件监听器,但现在我遇到了两件事: 仅当路由以前缀开始时加载另一个错误视图:/admin/ $route = $event->getRequest->get('_ro

我正在为一个网站构建一个管理面板,我想更改当404异常发生时调用的视图,但仅适用于管理应用程序<代码>(路径:/admin/*)

我已经对网站的
error404.html.twig
视图(位于
app/Resources/TwigBundle/views/Exception/
)做了过度修改

我想到了kernel.exception事件监听器,但现在我遇到了两件事:

  • 仅当路由以前缀开始时加载另一个错误视图:
    /admin/

    $route = $event->getRequest->get('_route')->render()
    //returns NULL
    
  • 调用
    $event->container->get('templating')->render()
    函数

当脚本失败时,我最终得到一个无限循环(空白页)

我唯一能做的就是:

  • 正在检索异常代码:

    $exception = $event->getException();
    $code = $exception->getCode();
    
  • 创建新的响应:

    $response = new Response();
    $event->setResponse($response);
    
对如何实现这一目标有何建议

[编辑] 班级:

namespace Cmt\AdminBundle\EventListener;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\TwigEngine;

class AdminActionListener
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var TwigEngine
     */
    protected $templating;


    /**
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container, TwigEngine $templating){
        // assign value(s)
        $this->container = $container;
        $this->templating = $templating;
    }

    /**
     * 
     * @param GetResponseForExceptionEvent $event
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // get exception
        $exception = $event->getException();

        // get path
        $path = $event->getRequest()->getPathInfo();

        /*
        * Redirect response to new 404 error view only
        * on path prefix /admin/ 
            */
        }
}
以及服务。yml:

services:
    cmt_admin.exception.action_listener:
        class: Cmt\AdminBundle\EventListener\AdminActionListener
        arguments: [@service_container] [@templating]
        tags:
            -   { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

你可以试试这个:

public function __construct(TwigEngine $templating)
{
    $this->templating = $templating;
}

public function onKernelException(GetResponseForExceptionEvent $event)
{
    static $handling;

    $exception = $event->getException();

    if (true === $handling) {
        return;
    }
    $handling = true;

    $code = $exception->getCode();

    if (0 !== strpos($event->getRequest()->getPathInfo(), '/admin') && 404 === $code) {
        $message = $this->templating->render('AcmeBundle:Default:error404new.html.twig', array());
        $response = new Response($message, $code);
        $event->setResponse($response);
    }

    $handling = false;
}
$templating变量可以在services.xml中传递:

<service id="acme.exception.listener" class="%acme.exception.listener.class%">
    <tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
    <argument type="service" id="templating" />
</service>

出于某种原因,这起作用了:

// get exception
$exception = $event->getException();

// get path
$path = $event->getRequest()->getPathInfo();

if ($exception->getStatusCode() == 404 && strpos($path, '/admin') === 0){

    $templating = $this->container->get('templating');

    $response = new Response($templating->render('CmtAdminBundle:Exception:error404.html.twig', array(
        'exception' => $exception
    )));

    $event->setResponse($response);
}
这基本上就是我之前用不同的语法做的


@不管怎样,谢谢你的帮助

嗨,谢谢你的回答。我试图传递$templating,但得到以下错误:
ErrorException:Catchable致命错误:传递给Cmt\AdminBundle\EventListener\AdminActionListener的参数1::\uu construct()必须是Symfony\Bundle\TwigBundle\TwigEngine的实例
您是否已添加到如上所示的服务中?嗯。此服务(“模板化”)在vendor/symfony/src/symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php中定义。因此,您可能安装了过时的Symfony2,或者以某种方式禁用了Twig。。您使用的Symfony的确切版本是什么?您可以检查我提到的文件吗?您还可以尝试删除显式类型检查,即:“公共函数构造($templating)”版本Symfony Standard 2.0.1。删除类型检查会引发此错误:
缺少Cmt\AdminBundle\EventListener\AdminActionListener::\uu construct()
我也尝试使用FrameworkExtension,但它不起作用。另外,命令
app/console container:debug
返回以下内容:
templating container->Symfony\Bundle\TwigBundle\TwigEngine