2013-01-11 24 views
0

我試圖將用戶轉發到位於另一個包中的自定義404頁面。我修改了我的ExceptionController並試圖將forward()頁面發送到我的其他錯誤控制器,該控制器被路由到我的自定義404頁面。Symfony 2 - 無法找到轉發()

我收到的錯誤: 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, 
     ) 
    ); 
} 

回答

0

我用docs中指出的快捷方式替換了forward()函數。

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

Symfony/Bundle/TwigBundle/Controller/ExceptionController不延伸控制器通常在用戶使用束的框架。此外,直接編輯存儲在vendor目錄中的任何內容都不是一個好主意。

+0

+1不用修改/ vendor中的文件。無論何時您通過作曲家更新供應商文件,您的更改都將丟失。 – Mike

相關問題