Twig 生成 url / 路由错误 / 函数未知
Twig generate url / Routing error / Function unknow
我正在使用 Silex 1.2 和 Twig 1.0。
我正在尝试使用 Twig 生成 URL。
<a href="{{ path('signUp') }}">Sign Up</a>
但是我得到这个错误:
Twig_Error_Syntax Unknown "path" function.
所以,就像这个 post (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;
你能帮帮我吗?
谢谢!
路径/signUp
与您在路径中提供的路由名称不同。你可能想要一个 named route 使用 bind
像这样:
$routes->get('/signUp', function () use ($app){
return $app['twig']->render('signUp.html.twig');
})->bind('signUp');
将此添加到 config/services.yaml
为我解决了这个问题:
services:
Twig\Environment:
calls:
- [addExtension, ['@Symfony\Bridge\Twig\Extension\RoutingExtension']]
Symfony\Bridge\Twig\Extension\RoutingExtension:
我正在使用 Silex 1.2 和 Twig 1.0。
我正在尝试使用 Twig 生成 URL。
<a href="{{ path('signUp') }}">Sign Up</a>
但是我得到这个错误:
Twig_Error_Syntax Unknown "path" function.
所以,就像这个 post (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;
你能帮帮我吗?
谢谢!
路径/signUp
与您在路径中提供的路由名称不同。你可能想要一个 named route 使用 bind
像这样:
$routes->get('/signUp', function () use ($app){
return $app['twig']->render('signUp.html.twig');
})->bind('signUp');
将此添加到 config/services.yaml
为我解决了这个问题:
services:
Twig\Environment:
calls:
- [addExtension, ['@Symfony\Bridge\Twig\Extension\RoutingExtension']]
Symfony\Bridge\Twig\Extension\RoutingExtension: