首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何覆盖微内核Symfony中的捆绑资源?

如何覆盖微内核Symfony中的捆绑资源?
EN

Stack Overflow用户
提问于 2016-06-09 09:34:43
回答 2查看 242关注 0票数 0

我有一个带有自定义目录结构的微内核Symfony项目。

我用了这个:https://github.com/ikoene/symfony-micro

我如何覆盖例如Twig资源(异常视图)?

Cookbook说,我应该在参考资料目录中创建一个名为TwigBundle的目录。

我创建了\AppBundle\Resources\TwigBundle\views\Exception目录。压倒一切的观点似乎行不通。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-10 17:39:13

感谢您使用微内核设置。下面是如何覆盖异常视图。

1.创建自定义ExceptionController

首先,我们将创建我们自己的ExceptionController,它扩展了基本ExceptionController。这将允许我们覆盖模板路径。

代码语言:javascript
复制
    <?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.重写默认的ExceptionController

现在,让我们指向配置中的新异常控制器。

twig: exception_controller: app.exception_controller:showAction

票数 1
EN

Stack Overflow用户

发布于 2016-06-10 20:36:04

我非常喜欢您的解决方案,但我找到了另一种方法,可以不使用自定义异常控制器。

我意识到,当您存储内核类时,目录资源中的目录资源中会自动进行额外的模板检查。

所以,在你的回购中的结构是:

代码语言:javascript
复制
/Resources/TwigBundle/views/Exception/

最后,我对目录结构做了一些修改,使其有一个包含内核文件的“app”目录。就像默认的Symfony项目一样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37722108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档