【发布时间】:2016-08-19 11:31:30
【问题描述】:
我按照说明here 做了一个自定义的error.html.twig(第三种情况)。我也做了一个自定义控制器。
我有两个问题:
第一个问题 我必须将自定义控制器放在哪里?我不明白这个文档:https://symfony.com/doc/current/controller/error_pages.html#overriding-the-default-exceptioncontroller
第二个问题 我正在尝试将 403、500、... 错误重定向到此页面。实际上,当您尝试访问某个文件夹时,您已“禁止访问”。
我试图在 .htaccess 中添加这个:ErrorDocument 403 http://www.xxx.fr/,但我不知道 404 页面的 URL 是什么。
编辑
好的,我知道将控制器放在哪里,但出现错误。
这是我的 ExceptionController.php
namespace Symfony\Bundle\TwigBundle\Controller;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ExceptionController
{
public function showAction()
{
$dir = $this->get('kernel')->getRootDir() . '/../web/images/theme/404/';
$dh = opendir($dir);
$errorFiles = array();
while (false !== ($filename = readdir($dh)))
if ($filename != '.' && $filename != '..' && $filename[0] != '.')
$errorFiles[] = $filename;
shuffle($errorFiles);
return $this->render('errors/show.html.twig', [
'gifs' => $errorFiles
]);
}
}
错误
哎呀,好像出了点问题。
ExceptionController.php 第 30 行中的 1/1 UndefinedMethodException: 试图调用类的名为“get”的未定义方法 “Symfony\Bundle\TwigBundle\Controller\ExceptionController”。在 ExceptionController.php 第 30 行
此控制器在经典视图中运行良好。
【问题讨论】:
-
1)
ExceptionController.php在AppBundle“控制器”文件夹中 -
快速浏览一下 Symfony 页面,我想说,您不需要任何额外的 Apache 指令。
-
@Olaf Dietsche 实际上,如果我尝试访问空 URL,我会收到“糟糕,发生错误”。但是,如果我试图访问一个文件夹(我做了 Options -Indexes),我会得到“禁止访问”。然后,它不是同一个页面;)
-
@pavlovich 它不起作用。我将 ExceptionController.php 放在我的 Controller 文件夹中,但出现此错误:第 29 行的@Twig/Exception/error.html.twig 中不存在变量“gifs”
-
根据 Symfony 页面,默认的异常控制器会首先查找特定的模板。所以我猜想某处有一个
error403.*文件,例如app/Resources/TwigBundle/views/Exception/error403.html.twig。如果您不想这样做,请删除或替换此文件。
标签: php apache .htaccess symfony redirect