【问题标题】:Symfony 2 - Cannot find Forward()Symfony 2 - 找不到 Forward()
【发布时间】:2013-01-11 00:25:34
【问题描述】:

我正在尝试将我的用户转发到位于另一个捆绑包中的自定义 404 页面。我修改了我的ExceptionController 并尝试将forward() 页面发送到我的另一个错误控制器,该控制器被路由到我的自定义404 页面。

我收到错误: Fatal error: Call to undefined method Symfony\Bundle\TwigBundle\Controller\ExceptionController::forward() in /home/notroot/www/store/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php on line 50

我正在修改文件store\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\Controller\ExceptionController.php

我在 ExceptionController.php 中添加了以下几行:

if ($code == '404') {
    $response = $this->forward('ItemBundle:Error:pageNotFound');
    return $response;
}

ExceptionController.php:

public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html')
{
    $this->container->get('request')->setRequestFormat($format);

    $currentContent = $this->getAndCleanOutputBuffering();

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


    if ($code == '404') {
        $response = $this->forward('ItemBundle:Error:pageNotFound');
        return $response;
    }

    return $templating->renderResponse(
        $this->findTemplate($templating, $format, $code, $this->container->get('kernel')->isDebug()),
        array(
            'status_code'    => $code,
            'status_text'    => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
            'exception'      => $exception,
            'logger'         => $logger,
            'currentContent' => $currentContent,
        )
    );
}

【问题讨论】:

    标签: symfony


    【解决方案1】:

    Symfony/Bundle/TwigBundle/Controller/ExceptionController 没有扩展用户包中通常使用的框架控制器。此外,直接编辑存储在vendor 目录中的任何内容都不是一个好主意。

    【讨论】:

    • +1 表示不修改 /vendor 中的文件。每当您通过 composer 更新您的供应商文件时,您的更改都会丢失。
    【解决方案2】:

    我将forward() 函数替换为它指向的快捷方式,如docs 中所述。

    if ($code == '404') {
        $httpKernel = $this->container->get('http_kernel');
        $response = $httpKernel->forward(
            'ItemBundle:Error:pageNotFound'
        );
        return $response;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 2013-05-23
      相关资源
      最近更新 更多