如何在 Slim3 Twig 中获取路由 link?
How to get route link in Slim3 Twig?
我这样定义我的路线:
$app->get('/about', function ($request, $response, $args) {
return $this->view->render($response, 'about.twig');
})->setName('about.page');
我有兴趣通过名称获取路线 link,例如:{% get_route('about.page') %}
我怎样才能做到这一点?
Is this possible?
是的。
IIRC
{{ path_for('about.page') }}
参考:
https://github.com/slimphp/Twig-View/blob/master/src/TwigExtension.php#L37
在 Slim3 中有 path_for(name)
功能。 F.ex:
{{ path_for('about.page') }}
参考:http://www.slimframework.com/docs/features/templates.html
The slim/twig-view
component exposes a custom path_for()
function to your Twig templates. You can use this function to generate complete URLs to any named route in your Slim application. The path_for()
function accepts two arguments:
- 1 A route name
- 2 A hash of route placeholder names and replacement values
注:path_for
使用路由器的功能$router->pathFor(..)
为什么不用{%
{%
是twig中的control structur,显示Message: Unknown "path_for" tag in "base.twig" at line XX.
是因为没有定义control structur这样的标签,所以twig不知道这其实是一个函数。
所以在 twig {{
.
中使用输出结构
我这样定义我的路线:
$app->get('/about', function ($request, $response, $args) {
return $this->view->render($response, 'about.twig');
})->setName('about.page');
我有兴趣通过名称获取路线 link,例如:{% get_route('about.page') %}
我怎样才能做到这一点?
Is this possible?
是的。
IIRC
{{ path_for('about.page') }}
参考:
https://github.com/slimphp/Twig-View/blob/master/src/TwigExtension.php#L37
在 Slim3 中有 path_for(name)
功能。 F.ex:
{{ path_for('about.page') }}
参考:http://www.slimframework.com/docs/features/templates.html
The
slim/twig-view
component exposes a custompath_for()
function to your Twig templates. You can use this function to generate complete URLs to any named route in your Slim application. Thepath_for()
function accepts two arguments:
- 1 A route name
- 2 A hash of route placeholder names and replacement values
注:path_for
使用路由器的功能$router->pathFor(..)
为什么不用{%
{%
是twig中的control structur,显示Message: Unknown "path_for" tag in "base.twig" at line XX.
是因为没有定义control structur这样的标签,所以twig不知道这其实是一个函数。
所以在 twig {{
.