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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题