1

我这样定义了我的路线:

$app->get('/about', function ($request, $response, $args) {
    return $this->view->render($response, 'about.twig');
})->setName('about.page');

我有兴趣按名称获取路线链接,例如:{% get_route('about.page') %}

我怎样才能做到这一点?

4

2 回答 2

4

在 Slim3 中有这个path_for(name)功能。前:

{{ path_for('about.page') }}

参考:http ://www.slimframework.com/docs/features/templates.html

该组件向您的 Twig 模板slim/twig-view公开了一个自定义函数。path_for()您可以使用此函数为您的 Slim 应用程序中的任何命名路由生成完整的 URL。该path_for()函数接受两个参数:

  • 1 路线名称
  • 2 路由占位符名称和替换值的哈希

注意:path_for使用路由器的功能是$router->pathFor(..)

为什么不使用{%

{% is a control structur in twig, Message: Unknown "path_for" tag in "base.twig" at line XX. is displayed because there is no such tag as control structur defined so twig doesn't know that this is actually a function. So use the output structur in twig {{.

于 2016-11-01T19:14:58.687 回答
1

这可能吗?

是的。

IIRC

{{ path_for('about.page') }}

参考:

https://github.com/slimphp/Twig-View/blob/master/src/TwigExtension.php#L37

于 2016-11-01T17:29:34.950 回答