Skip to content
Advertisement

Url query parameters becoming visibly encoded upon redirect – Symfony 2

I am attempting to persist my query parameters when redirecting to a new path with Symfony2, however the method I am using appears to show encoded query parameters in the URL…

return $this->redirect(
    $this->generateUrl(
        'my_page_name',
        [
            'myVar' => $myVar,
            $request->query->all() 
            // I have also tried $request->getQueryString() with the same result...
        ]
    )
);

When sending an URL with query params such as

my-domain.com/some-page?var1=test&var2=test

I can visibly see the URL change in the browser on redirect to something resembling

my-domain.com/some-page?0var1%3Dtest%26var2%3Dtest

My question here is…

1) Does this affect the actual URL being navigated to 2) Is there any way in which I can stop this and keep the URL looking as it was entered?

Advertisement

Answer

It is encoding request parameters because you are passing whole array as parameter which in terms gets serialized, to avoid that you need to pass each parameter individually. This can be achieved by merging arrays.

So solution would look like :

return $this->redirect(
    $this->generateUrl(
        'my_page_name',
        array_merge(
            array(
                'myVar' => $myVar, 
                'myVar2' => $myVar2
            ),
            $request->query->all()
      ) 
    )
);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement