0
votes

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
Mayby in base.html.twig twig_array_merge throw error ? ...Gregsparrow
No, I don't use twig_array_merge at all.Gottlieb Notschnabel
@Gregsparrow is on the right track. I can reproduce the error with 'MyappBundle::base.html.twig'. Try moving the base template into your bundle, e.g. 'MyappBundle:Default:base.html.twig'geoB
Doesn't apply in my case. If I file MyappBundle::base.html.twig to MyappBundle:Default:base.html.twig the error remains.Gottlieb Notschnabel
So, to confirm: error persists after 1) moving base template, 2) modifying error template, and 3) clearing prod cache?geoB

1 Answers

1
votes

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 %}