How to pass parameters to route in Twig?
Created 7 years ago by drmonkeyninja

I'm trying to pass parameters to a route in Twig.

In blade I would use:-

{{ route('clients.mail', [$client->id, $month]) }}

How do I do this in Twig? I've tried using:-

{{ client.route('mail', [month]) }}

This fails. The route I have defined in my service provider is:-

'clients/{id}/mail/{month}' => [
    'verb' => 'get',
    'as' => 'default.module.support::clients.mail',
    'uses' => 'Default\SupportModule\Http\Controller\ClientsController@mail'
],

month is a parameter that is being passed into the view based on a filter the page uses.

ryanthompson  —  7 years ago Best Answer

I think url_route($route, $parameters) is what you're looking for. url_* maps methods to Laravel's URLGenerator.

ryanthompson  —  7 years ago
drmonkeyninja  —  7 years ago

Thanks Ryan, that does the trick. I'd tried just about everything but that.

{{ url_route('default.module.support::clients.mail', [client.id, month]) }}
ryanthompson  —  7 years ago

Now you know 😊

Keep in mind all those similar functions are available. session_*, str_*, as well as route_* and some others: https://pyrocms.com/documentation/streams-plugin/latest