doujie4344 2015-01-11 13:28
浏览 46
已采纳

自定义错误模板会引发致命错误

I created the file

/myapp/app/Resources/TwigBundle/views/Exception/error.html.twig

with this content

{% extends 'MyappBundle::base.html.twig' %}

{% block body %}
<div>
    <h1>Error</h1>
    <p>The server returned a "{{ status_code }} {{ status_text }}".</p>
</div>
{% endblock %}

This is how I extend all templates in my app, and it works without any problem. But it doesn't work for the error handling. I always get this error message:

Fatal error: Uncaught exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' with message 'Unable to find entity.' in C:\xampp\htdocs\myapp\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php:218 Stack trace: #0 C:\xampp\htdocs\myapp\src\myapp\MyappBundle\Controller\AddressController.php(207): Symfony\Bundle\FrameworkBundle\Controller\Controller->createNotFoundException('Unable to find ...') #1 [internal function]: myapp\MyappBundle\Controller\AddressController->showAction('=k21RBNjM') #2 C:\xampp\htdocs\myapp\app\bootstrap.php.cache(3020): call_user_func_array(Array, Array) #3 C:\xampp\htdocs\myapp\app\bootstrap.php.cache(2982): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #4 C:\xampp\htdocs\myapp\app\bootstrap.php.cache(3131): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #5 C:\xampp\htdocs\myapp\app\bootstrap.php.cache(2376) in C:\xampp\htdocs\myapp\app\bootstrap.php.cache on line 2998

This is how I throw the 404 error:

$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyBundle:Entity')->find($id);
if (!$entity) {
    throw $this->createNotFoundException('Unable to find entity.');
}

I am using Win7 with PHP v5.5.6 and Symfony v2.6.3. When running on my production server, I get a blank page (no sourcecode at all). And –surely– I've cleared my cache.

Update

When I try to preview my error page in dev mode, I get this error message:

The merge filter only works with arrays or hashes; NULL and array given in TwigBundle:Exception:error.html.twig at line 1.

I don't use twig_array_merge at all.

Update 2

This is caused by a path() call where I merge the _route_params and the _locale in my footer.html.twig that I include from my MyappBundle::base.html.twig file:

<a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': lang})) }}"><span class="flag-icon flag-icon-{{ lang }}"></span> {{ lang }}</a>

It works on all my templates except on the error template. What is wrong here, how can I fix that?

  • 写回答

1条回答 默认 最新

  • dpaal28266 2015-01-12 15:09
    关注

    The simplest solution, although not necessarily the most desirable, is to modify the line in Update 2 as follows:

    {% if app.request.get('_route_params') is not null %}
    <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': lang})) }}"><span class="flag-icon flag-icon-{{ lang }}"></span> {{ lang }}</a>
    {% endif %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程