43

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.

Mo.
  • 26,306
  • 36
  • 159
  • 225
Piotr
  • 457
  • 5
  • 7
  • 1
    I've researched this a bit and it looks like its a dead-end with Silex. It seems to either be a bug or Silex just doesn't support registered helpers within the error handler. – lifo Dec 13 '12 at 15:36
  • Since the problem creates an RouteNotFound Error and you already specified it has probably something to do with the url function from the twig bridge, have you already tried using the standard notaion of UrlGeneratorServiceProvider such as app.url_generator.generate('home')? – oshell Aug 28 '14 at 08:50
  • Not 100% sure about Silex, but in Symfony when using custom error templates, they are in fact ignored if the templates (or anything within the handler) throw exceptions or errors. This might be the case with you. Try rendering the error template at some debug route and see if that is the case. You should try debugging this step-by-step with XDebug. It will give you a better understanding of how it works anyway. – Gerard van Helden May 30 '15 at 10:34

1 Answers1

1

This is not a Silex problem (as of now) - Everything works perfectly on my side (Silex 1.2)

Did you register the UrlGeneratorServiceProvider in your app ?

in web/index.php:

$app->register(new Silex\Provider\UrlGeneratorServiceProvider());

And you should really use path() instead of url()in this case :

{{ path('home') }}
tchap
  • 3,412
  • 3
  • 29
  • 46