Twig生成url /路由错误/功能未知

时间:2017-11-26 16:21:17

标签: php symfony twig silex

我使用的是Silex 1.2和Twig 1.0。

我尝试使用Twig生成网址。

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

但是我收到了这个错误:

Twig_Error_Syntax Unknown "path" function.

所以,就像在这篇文章(Silex - Twig_Error_Syntax: The function "path" does not exist)上一样,我在boostrap.php上添加了此内容。

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

但是我收到了这个错误:

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

路由文件

<?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;

你能帮助我吗?

感谢&#39; s!

2 个答案:

答案 0 :(得分:0)

路径/signUp与您在路径中提供的路径名称不同。您可能需要使用bind这样的named route

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

答案 1 :(得分:0)

将此添加到config/services.yaml可以为我解决:

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

    Symfony\Bridge\Twig\Extension\RoutingExtension: