我有一个带有自定义目录结构的微内核Symfony项目。
我用了这个:https://github.com/ikoene/symfony-micro
我如何覆盖例如Twig资源(异常视图)?
Cookbook说,我应该在参考资料目录中创建一个名为TwigBundle的目录。
我创建了\AppBundle\Resources\TwigBundle\views\Exception目录。压倒一切的观点似乎行不通。
发布于 2016-06-10 17:39:13
感谢您使用微内核设置。下面是如何覆盖异常视图。
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.创建错误模板
为不同的错误代码创建模板:
在本例中,异常模板将放置在AppBundle/Resources/views/Exception/中。
3.重写默认的ExceptionController
现在,让我们指向配置中的新异常控制器。
twig: exception_controller: app.exception_controller:showAction
发布于 2016-06-10 20:36:04
我非常喜欢您的解决方案,但我找到了另一种方法,可以不使用自定义异常控制器。
我意识到,当您存储内核类时,目录资源中的目录资源中会自动进行额外的模板检查。
所以,在你的回购中的结构是:
/Resources/TwigBundle/views/Exception/最后,我对目录结构做了一些修改,使其有一个包含内核文件的“app”目录。就像默认的Symfony项目一样。
https://stackoverflow.com/questions/37722108
复制相似问题