1

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 !

Royce
  • 45
  • 1
  • 1
  • 9

2 Answers2

0

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');
dbrumann
  • 16,803
  • 2
  • 42
  • 58
0

Adding this to config/services.yaml solved it for me:

services:
    Twig\Environment:
        calls:
            - [addExtension, ['@Symfony\Bridge\Twig\Extension\RoutingExtension']]

    Symfony\Bridge\Twig\Extension\RoutingExtension:
darkdragon
  • 392
  • 5
  • 13