Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silex & Twig helpers in custom error pages

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.

like image 311
Piotr Avatar asked Oct 24 '12 22:10

Piotr


People also ask

What is Silex Technology on my wifi?

Silex works with multi-national OEMs whose end users rely on wireless connectivity to improve patient care in medical applications, boost productivity in industrial applications, and provide seamless wireless networking and display for commercial applications.

What is a Silex server?

Silex Technology DS-510 Gigabit USB Device Server easily connects and shares USB devices over a network. Using the DS-510, printers, scanners, disk drives, card readers, and virtually any other USB device can be enabled with network capability.

What is Silex Company?

A specialist site dedicated to collecting vintage appliances. Well, not just any appliances, but toasters. And not just any toasters, but Sunbeam Radiant Control toasters. Decodan's Vintage Appliances.

What products does silex technology make?

Our robust, secure servers and bridges provide fast network speeds, connection versatility, and enterprise security. Plus, we offer WLAN network analyzers and device networking software as powerful tools for monitoring and managing your network and connected devices.


1 Answers

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') }} 
like image 197
tchap Avatar answered Oct 13 '22 21:10

tchap