Silex & Twig Helpers in Custom Error Pages

I am struggling with a problem while trying to make 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 customize the 404 error page in my application. Everything works fine until I start using helpers in my branch template.

Sample code for the 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 handling errors in my Silex application:

 $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); }); 

As soon as i remove

  {{url ('home')}} 
(This helper and route work fine in other cases!) I get the appropriate site rendering, but without translations.

With the assistant, 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 

Therefore, I need to be guided here, what could be a possible reason for this, what causes it, and steps to solve this problem. All help was appreciated.

+43
url twig fatal-error helpers silex
Oct 24
source share
1 answer

This is not a Silex problem (for now) - everything works fine on my side (Silex 1.2)

Have you registered UrlGeneratorServiceProvider in your application?

in web/index.php :

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

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

 {{ path('home') }} 
+1
Sep 30 '14 at 10:59
source share



All Articles