Michelle Michelle - 1 month ago 10
Twig Question

How to get route link in Slim3 Twig?

I defined my route so:

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


I'm interested to get route link by name like:
{% get_route('about.page') %}


How can I achieve this?

Answer

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

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

Note: The path_for uses the function of the router which is $router->pathFor(..)

Reference: 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