Symfony 2 - 找不到 Forward() [英] Symfony 2 - Cannot find Forward()
问题描述
我正在尝试将我的用户转发到位于另一个包中的自定义 404 页面.我修改了我的 ExceptionController
并尝试将页面 forward()
转到我的另一个错误控制器,该控制器路由到我的自定义 404 页面.
I am trying to forward my users to a custom 404 page which is located in another bundle. I have modified my ExceptionController
and attempted to forward()
the page to my other Error controller which is routed to my custom 404 page.
我收到错误消息:致命错误:调用/home/notroot/www/store/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Controller/中未定义的方法 Symfony\Bundle\TwigBundle\Controller\ExceptionController::forward()ExceptionController.php 第 50 行
我正在修改文件 store\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\Controller\ExceptionController.php
.
我在 ExceptionController.php 中添加了以下几行:
I have added the following lines to ExceptionController.php:
if ($code == '404') {
$response = $this->forward('ItemBundle:Error:pageNotFound');
return $response;
}
ExceptionController.php:
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,
)
);
}
推荐答案
我将 forward()
函数替换为它指向的快捷方式,如 文档.
I replaced the forward()
function with the shortcut it points to as stated in the docs.
if ($code == '404') {
$httpKernel = $this->container->get('http_kernel');
$response = $httpKernel->forward(
'ItemBundle:Error:pageNotFound'
);
return $response;
}
这篇关于Symfony 2 - 找不到 Forward()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!