渲染控制器异常模板

渲染控制器异常模板

本文介绍了Symfony 渲染控制器异常模板(“")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在树枝中渲染一个控制器.我遵循文档嵌入其他控制器".

我经常通过 render(controller()) 方法得到一个例外:渲染模板(")时抛出异常

在我的 template.html.twig 中

{{ render(controller('MyBundle:User:logo')) }}

在我的控制器中

class UserController extends Controller {公共函数 logoAction(){return $this->render('MyBundle:User:logo.html.twig', []);}}

如果我在我的模板中使用它效果很好

{% 渲染 url('logo') %}

随路线

标志:图案:/标志默认值:{ _controller: MyBundle:User:logo }

但我想使用 render(controller())

我做错了什么?

编辑,添加有关异常的更多信息:

在 MyBundle:My/Dashboard:dashboard.html.twig 第 51 行渲染模板 ("") 期间抛出异常.500 内部服务器错误 - Twig_Error_Runtime 1 链接异常:ResourceNotFoundException »[2/2] Twig_Error_Runtime:在 MyBundle:My/Dashboard:dashboard.html.twig 第 51 行渲染模板 ("") 期间抛出异常.-在 app/cache/dev/classes.php 第 7378 行 - } catch (Twig_Error $e) { throw $e;} catch (Exception $e) { throw new Twig_Error_Runtime(sprintf('在渲染模板(%s")时抛出异常.', $e->getMessage()), -1, $template->getTemplateName(), $e);} } elseif (false !== $parent = $this->getParent($context)) { $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks), false);在 Twig_Template-> displayBlock ('body', array('preset' => array('date' => '2015-03-31'),... 1/2] ResourceNotFoundException: -在 app/cache/dev/appDevUrlMatcher.php 第 632 行 -}抛出 0 <计数($允许)?new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();在 appDevUrlMatcher ->match ('/_fragment') in app/cache/dev/classes.php at line 3278 + at Router ->match ('/_fragment')

Edit2 dashboard.html.twig

{% 块体 %}<div class="panel panel-default"><div class="panel-heading"><span class="glyphicon glyphicon-random" aria-hidden="true"></span><b>{{ "Request"|trans }}</b>

<div class="panel-body"><form role="form" action="{{ path('dashboard', {'idDbd': idDbd}) }}"><div class="form-inline">{{ render(controller('MyBundle:User:logo')) }}

{% 包括 'MyBundle:My/Request:request_edit.html.twig' %}</表单>

(……)

解决方案

为了能够对子请求使用 controller() 表示法,您需要启用特殊的 FragmentListener 它将能够处理此类子请求.

你需要在你的 config.yml 文件中有它的配置.这是在新的 Symfony 项目中默认完成的:https://github.com/symfony/symfony-standard/blob/7311e7065bea8986f8eb2e95ab9dd4a55ce95ae2/app/config/config.yml#L31检查你自己的项目中是否有这行

I tried to render a controller inside twig. I follow the documentation "Embedding other Controllers".

I constantly get an exeption with the method render(controller()):An exception has been thrown during the rendering of a template ("")

In my template.html.twig

{{ render(controller('MyBundle:User:logo')) }}

In my controller

class UserController extends Controller {
    public function logoAction()
    {
        return $this->render('MyBundle:User:logo.html.twig', []);
    }
}

It work well if I use in my template

{% render url('logo') %}

With the route

logo:
    pattern:  /logo
    defaults: { _controller: MyBundle:User:logo }

But I want to use render(controller())

What do I do wrong?

Edit, add more info about exception:

An exception has been thrown during the rendering of a template ("") in MyBundle:My/Dashboard:dashboard.html.twig at line 51.

500 Internal Server Error - Twig_Error_Runtime 1 linked Exception: ResourceNotFoundException »

[2/2] Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("") in MyBundle:My/Dashboard:dashboard.html.twig at line 51.   -

in app/cache/dev/classes.php at line 7378   - } catch (Twig_Error $e) { throw $e; } catch (Exception $e) { throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $template->getTemplateName(), $e); } } elseif (false !== $parent = $this->getParent($context)) { $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks), false); at Twig_Template
->displayBlock ('body', array('preset' => array('date' => '2015-03-31'),... 1/2] ResourceNotFoundException:    -

in app/cache/dev/appDevUrlMatcher.php at line 632   -
        }
        throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
    } } at appDevUrlMatcher ->match ('/_fragment')  in app/cache/dev/classes.php at line 3278   + at Router ->match ('/_fragment')

Edit2 dashboard.html.twig

{% block body %}
    <div class="panel panel-default">
      <div class="panel-heading">
        <span class="glyphicon glyphicon-random" aria-hidden="true"></span>
        <b>{{ "Request"|trans }}</b>
      </div>
      <div class="panel-body">
          <form role="form" action="{{ path('dashboard', {'idDbd': idDbd}) }}">
                <div class="form-inline">
                    {{ render(controller('MyBundle:User:logo')) }}
                </div>
                {% include 'MyBundle:My/Request:request_edit.html.twig' %}
          </form>
      </div>
    </div>
    (...)
解决方案

To be able to use the controller() notation for subrequests, you need to enable the special FragmentListener which will be able to handle such subrequests.

You need to have the config for it in your config.yml file. This is done by default in new Symfony project: https://github.com/symfony/symfony-standard/blob/7311e7065bea8986f8eb2e95ab9dd4a55ce95ae2/app/config/config.yml#L31Check whether you have this line in your own project

这篇关于Symfony 渲染控制器异常模板(“")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:59