user2143356
user2143356

Reputation: 5597

Simple redirect in Symfony2 by changing a query string parameter, NOT route related

This should be simple, and I've been searching all over Google, but I keep coming up with 'route' related advice.

I just want to perform a redirect to the same page and modify one of the query string parameters (either to clear one or set one).

I can't see how to do this anywhere.

An option could be to completely generate the URL manually and use this I guess, but that doesn't seem a very good method:

$this->router->generate("http://domain.com?a=1")

Upvotes: 5

Views: 5438

Answers (2)

Federkun
Federkun

Reputation: 36924

I hope I understand what you intend to do... In your controller (?) use

$this->generateUrl(
    $request->attributes->get('_route'),
    array_merge(
        $request->query->all(),
        array('param' => 'val') // change the param
    )
);

to generate the url.

Upvotes: 10

NHG
NHG

Reputation: 5877

What is the reason of this redirect? I suppose that you wanna redirect from controller, don't you? I'm not sure what result you wanna achive. You have to be careful with redirecting in controller for same action controller (redirect loop).

However, in controller you can do that by:

public function indexAction()
{
    // ...
    return $this->redirect($this->generateUrl($request->attributes->get('_route'), array('paramName' => $paramValue)));
}

In my opinion, you should consider writing an event listener: http://symfony.com/doc/current/book/internals.html#handling-requests

Upvotes: 0

Related Questions