번들별로 예외를 사용자 정의하는 방법은 무엇입니까?번들로 예외 템플릿 사용자 정의
예 :
두 개의 묶음 : BackendBundle과 FrontEndBundle이 있습니다. error 404
이 던져 질 때이 두 번들을 두 개의 다른 템플릿으로 처리하기를 원합니다.
어떻게하면됩니까?
나는 을 읽었지만 여전히 단서를 얻지 못했습니다.
번들별로 예외를 사용자 정의하는 방법은 무엇입니까?번들로 예외 템플릿 사용자 정의
예 :
두 개의 묶음 : BackendBundle과 FrontEndBundle이 있습니다. error 404
이 던져 질 때이 두 번들을 두 개의 다른 템플릿으로 처리하기를 원합니다.
어떻게하면됩니까?
나는 을 읽었지만 여전히 단서를 얻지 못했습니다.
KernelEvents::EXCEPTION
이벤트에 연결하여 브라우저에 보낼 응답을 무시할 수 있습니다. 나는 당신을 위해 빠른 요점을 썼다 :
https://gist.github.com/bezhermoso/87716a9c72a1d12c5036
그러나, $event->getRequest()->get('_controller')
분명히, 404 오류에 null
를 반환합니다. 그래서 그 인스턴스를 설명해야합니다.
언급 한 cookbook article에서와 마찬가지로 TwigBundle
과 Symfony\Bundle\TwigBundle\Controller\ExceptionController:findTemplate
을 확장하십시오. 거기에서 결정할 수 있습니다 (디버그되지 않은 경우) 어떤 404 보여줄 수 있습니다.
이 예제에서는 모든 백엔드 경로가 /backend
에 도달 할 수 있다고 가정합니다. 필요에 따라 변경하거나 요청의 다른 것들을 사용하여 백엔드 404를 결정하십시오.
namespace Acme\ErrorBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseController;
/**
* ExceptionController.
*/
class ExceptionController extends BaseController
{
/**
* @param Request $request
* @param string $format
* @param integer $code An HTTP response status code
* @param Boolean $debug
*
* @return TemplateReference
*/
protected function findTemplate(Request $request, $format, $code, $debug)
{
// find template for backend 404 errors
if (!$this->debug && 404 == $code && false !== strpos($request->getPathInfo(), '/backend')) {
$template = new TemplateReference('TwigBundle', 'Exception', 'backend404', $format, 'twig');
if ($this->templateExists($template)) {
return $template;
}
}
// the parent method finds the error404.html.twig for the frontend
return parent::findTemplate($request, $format, $code, $debug);
}
}
또한, ErrorBundle은 TwigBundle을 상속해야합니다.
오류 페이지 또는 예외 페이지 (dev로 표시)를 의미합니까? –
@Pazi : 우리가'createNotFoundException()'을 던져 넣었을 때의 오류 페이지를 의미합니다 – GusDeCooL