自定义错误模板抛出致命错误
Custom error template throws fatal error
我创建了文件
/myapp/app/Resources/TwigBundle/views/Exception/error.html.twig
有这个内容
{% extends 'MyappBundle::base.html.twig' %}
{% block body %}
<div>
<h1>Error</h1>
<p>The server returned a "{{ status_code }} {{ status_text }}".</p>
</div>
{% endblock %}
这就是我在我的应用程序中扩展所有模板的方式,而且它没有任何问题。但它不适用于错误处理。我总是收到此错误消息:
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
这是我抛出 404 错误的方式:
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyBundle:Entity')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find entity.');
}
我正在使用带有 PHP v5.5.6 和 Symfony v2.6.3 的 Win7。当 运行 在我的生产服务器上时,我得到一个空白页面(根本没有源代码)。而且——当然——我已经清除了我的缓存。
更新
当我尝试 preview my error page in dev mode 时,我收到此错误消息:
The merge filter only works with arrays or hashes; NULL and array given in TwigBundle:Exception:error.html.twig at line 1.
我根本不使用 twig_array_merge
。
更新 2
这是由 path()
调用引起的,我在其中合并了 footer.html.twig
中的 _route_params
和 _locale
,我从 MyappBundle::base.html.twig
文件中包含了这些:
<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>
它适用于我的所有模板,错误模板除外。这里有什么问题,我该如何解决?
最简单的解决方案(虽然不一定是最理想的)是按如下方式修改更新 2 中的行:
{% 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 %}
我创建了文件
/myapp/app/Resources/TwigBundle/views/Exception/error.html.twig
有这个内容
{% extends 'MyappBundle::base.html.twig' %}
{% block body %}
<div>
<h1>Error</h1>
<p>The server returned a "{{ status_code }} {{ status_text }}".</p>
</div>
{% endblock %}
这就是我在我的应用程序中扩展所有模板的方式,而且它没有任何问题。但它不适用于错误处理。我总是收到此错误消息:
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
这是我抛出 404 错误的方式:
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyBundle:Entity')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find entity.');
}
我正在使用带有 PHP v5.5.6 和 Symfony v2.6.3 的 Win7。当 运行 在我的生产服务器上时,我得到一个空白页面(根本没有源代码)。而且——当然——我已经清除了我的缓存。
更新
当我尝试 preview my error page in dev mode 时,我收到此错误消息:
The merge filter only works with arrays or hashes; NULL and array given in TwigBundle:Exception:error.html.twig at line 1.
我根本不使用 twig_array_merge
。
更新 2
这是由 path()
调用引起的,我在其中合并了 footer.html.twig
中的 _route_params
和 _locale
,我从 MyappBundle::base.html.twig
文件中包含了这些:
<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>
它适用于我的所有模板,错误模板除外。这里有什么问题,我该如何解决?
最简单的解决方案(虽然不一定是最理想的)是按如下方式修改更新 2 中的行:
{% 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 %}