Mario
Mario

Reputation: 3379

Generate URL and keep GET parameters Symfony2

I searching for the Symfony way of handling keeping the current GET parameters.

lets say the current url is: http://example.com/cars/?q=Volvo%p=1

Now i have to generate a url based on the current one but modify a parameter: http://example.com/cars/?q=Volvo%p=2

Im searching for the symfony best practice solution for such a case, it can't be the right way simply generating the url by appending all known get parameters by hand.

Doing something like:

$this->generateUrl( 'some_route', array( 'q' => $request->get('q'), 'p' => $request->get('p') ) );

or

$this->generateUrl( 'some_route', array_merge( $request->query->all(), array( 'p' => $request->get('p') + 1  ) ) );

just feels wrong.

There has to be a nice and clean solution for this. Im thinking of something like this:

<!-- path( <route>, <params>, <persist current> ) -->
{{ path( 'some_route', { p: 2 }, true ) }}

The fuelPHP framework for example has the update_query_string method for this.

Upvotes: 2

Views: 3436

Answers (2)

totas
totas

Reputation: 10798

Here's what I did:

{{ url('my_route', {myparam:'value'}|merge(app.request.query.all)) }}

Upvotes: 3

xurshid29
xurshid29

Reputation: 4210

You can do it like this:

$this->generateUrl('some_route', $request->query->all())

Upvotes: 0

Related Questions