How to pass parameters to route in Twig?
Created 6 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' => '',
    '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  —  6 years ago Best Answer

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

ryanthompson  —  6 years ago
drmonkeyninja  —  6 years ago

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

{{ url_route('', [, month]) }}
ryanthompson  —  6 years ago

Now you know 😊

Keep in mind all those similar functions are available. session_*, str_*, as well as route_* and some others: