view raw
Michelle Michelle - 6 months ago 64
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');

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

How can I achieve this?


{% 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('') }}

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


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