path_for ignores array parameters


I’m trying to build a URL to be used for language switching and path_for, which is being called like this:

{{ path_for('home', { 'lang': 'en' }) }}

simple generates a link to “/” thus ignoring the expected lang=en part which should be generated. Chances are it’s me, so I have to ask: What am I doing wrong?

Once again, thanks for and hints and tips.

If you look at the pathFor method you see query params is the 3rd parameter. Dont know about your template function path_for.

Is home a named route? ->setName('home')

Thanks for the responses guys. @tflight, yes it’s a named route.

@JoeBengalen: I looked at , specifically the example given under the heading “The path_for() method” which references the built-in path_for() twig extension.

I’ll experiment a bit. Could be a case of the docs being not quite 100% correct (or me being stupid).

Thanks for your replies guys.

If this is the same app as you posted the route definitions for in the other thread, then you are not specifying where the lang param should go in the route definition.

So something like…
$app->get ('/?lang={lang}', 'App\Controller\UserController:home')->setName ('home');

to give you /lang=?en


$app->get ('/{lang}', 'App\Controller\UserController:home')->setName ('home');

to give you /en

@TheLobos Docs are fine. There is an example shown where the $data sent into path_for has a placeholder {name} in the route. What you are trying to do is different. You want to add a query parameter, which is the 3rd parameter of path_for.

I think this will do:

{{ path_for('home', {}, { 'lang': 'en' }) }}
1 Like

Confirmed, doing it that way works like a charm. Sorry for my bad doc-reading :slight_smile: