我正在尝试将用户转发到位于另一个捆绑包中的自定义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,
)
);
}
答案 0 :(得分:4)
Symfony/Bundle/TwigBundle/Controller/ExceptionController
不会扩展通常在用户捆绑中使用的框架控制器。此外,直接编辑存储在vendor
目录中的任何内容永远不是一个好主意。
答案 1 :(得分:0)
我将forward()
函数替换为docs中所述的快捷方式。
if ($code == '404') {
$httpKernel = $this->container->get('http_kernel');
$response = $httpKernel->forward(
'ItemBundle:Error:pageNotFound'
);
return $response;
}