Благодарим за использование установки микроядра. Вот как переопределить представления исключений.
1. Создание пользовательского ExceptionController
Во-первых, мы собираемся создать наш собственный ExceptionController, который расширяет базовый ExceptionController. Это позволит нам перезаписать путь к шаблону.
<?php
namespace AppBundle\Controller\Exception;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
use Symfony\Component\HttpFoundation\Request;
class ExceptionController extends BaseExceptionController
{
/**
* @param Request $request
* @param string $format
* @param int $code
* @param bool $showException
*
* @return string
*/
protected function findTemplate(Request $request, $format, $code, $showException)
{
$name = $showException ? 'exception' : 'error';
if ($showException && 'html' == $format) {
$name = 'exception_full';
}
// For error pages, try to find a template for the specific HTTP status code and format
if (!$showException) {
$template = sprintf('AppBundle:Exception:%s%s.%s.twig', $name, $code, $format);
if ($this->templateExists($template)) {
return $template;
}
}
// try to find a template for the given format
$template = sprintf('@Twig/Exception/%s.%s.twig', $name, $format);
if ($this->templateExists($template)) {
return $template;
}
// default to a generic HTML exception
$request->setRequestFormat('html');
return sprintf('@Twig/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
}
}
2. Создание шаблонов ошибок
Создание шаблонов для различных кодов ошибок:
- error.html.twig
- error403.html.twig
- error404 .html.twig
В этом примере шаблоны исключения будут помещены в AppBundle/Resources/views/Exception/
3. Override по умолчанию ExceptionController
Теперь давайте точку к нашему новому контроллеру исключений в конфигурации.
twig: exception_controller: app.exception_controller:showAction