Search code examples
phpsymfonytwigsilex

Twig generate url / Routing error / Function unknow


I'm using Silex 1.2 and Twig 1.0.

I'm trying to generate an URL with Twig.

<a href="{{ path('signUp') }}">Sign Up</a>

However I get this error :

Twig_Error_Syntax Unknown "path" function.

So, like on this post (Silex - Twig_Error_Syntax: The function "path" does not exist) I added this on my boostrap.php.

$app->register(new UrlGeneratorServiceProvider());
$app['twig']->addFunction(new \Twig_SimpleFunction('path', function($url) use ($app) {
    return $app['url_generator']->generate($url);
}));

But I get this error :

RouteNotFoundException Unable to generate a URL for the named route "signUp" as such route does not exist.

Routing file

<?php

$routes = $app['controllers_factory'];

$routes->get('/', function () use ($app){
    return $app['twig']->render('home.html.twig');
});

$routes->get('/signUp', function () use ($app){
    return $app['twig']->render('signUp.html.twig');
});

$routes->get('/signIn', function () use ($app) {
    return $app['twig']->render('signIn.html.twig');
});

return $routes;

Can you help me ?

Thank's !


Solution

  • The path /signUp is not the same as the route name you provide in path. You probable want a named route using bind like this:

    $routes->get('/signUp', function () use ($app){
        return $app['twig']->render('signUp.html.twig');
    })->bind('signUp');