I'm struggling with a problem while trying to render a custom error page in Silex
.
According to what I found in this link :http://refactoring.us/silex/custom-error-pages-with-silex-and-twig/
I am trying to set up a custom 404 error page in my application. Everything works fine until I start to use helpers in my twig template.
An exemplary code for 404 error page template is as follows :
{% extends "layout.html.twig" %}
{% block main %}
<div id="error404">
<h2>{{ app.translator.trans('page404.title') }}</h2>
<p>{{ app.translator.trans('page404.para1') }}</p>
<p class="btn-footer">
<a href="{{ url('home') }}" class="btn">{{ app.translator.trans('page404.button') }}</a>
</p>
</div>
{% endblock %}
PHP code for error handling in my Silex app:
$app->error(function (\Exception $e, $code) use($app) {
switch ($code) {
case 404:
$message = $app['twig']->render('error404.html.twig');
break;
default:
$message = $app['twig']->render('error500.html.twig');
}
return new Response($message, $code);
});
Once i remove
{{ url('home') }}(this helper and route works perfectly in other cases!) I get the proper rendered site, but without the translations.
With the helper, I get the following error:
Fatal error: Uncaught exception 'Symfony\Component\Routing\Exception\RouteNotFoundException' with message 'Route "" does not exist.' in D:\projects\projectname\application\vendor\symfony\routing\Symfony\Component\Routing\Generator\UrlGenerator.php:119 Stack trace:
#0 D:\projects\projectname\application\vendor\symfony\twig-bridge\Symfony\Bridge\Twig\Extension\RoutingExtension.php(45): Symfony\Component\Routing\Generator\UrlGenerator->generate(NULL, Array, false)
#1 D:\projects\projectname\application\vendor\twig\twig\lib\Twig\Environment.php(327) : eval()'d code(68): Symfony\Bridge\Twig\Extension\RoutingExtension->getPath(NULL, Array)
#2 D:\projects\projectname\application\vendor\twig\twig\lib\Twig\Template.php(265): __TwigTemplate_ca53e56b87abd45da5c34a79d4c2ce34->doDisplay(Array, Array)
#3 D:\projects\projectname\application\vendor\twig\twig\lib\Twig\Template.php(239): Twig_Template->displayWithErrorHandling(Array, Array)
#4 D:\projects\projectname\application\vendor\twig\twig\lib\Twig\Envir in D:\projects\projectname\application\vendor\twig\twig\lib\Twig\Template.php on line 280
So I need some guidance here on what could be the possible reason behind this that is causing this and steps to resolve this issue. All help appreciated.