2

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.

4
  • Do you want to do a pagination? Does this GET parameters come from a form? Commented Feb 17, 2015 at 14:01
  • Yes but not all get parameters are from the pagination. Thats why i have to persist all of them. Commented Feb 17, 2015 at 14:05
  • Where does the get parameters come from? From the page, a form, session? Commented Feb 17, 2015 at 14:05
  • Forms, links, handwritten basically they could be from everywhere. Just every parameter inside $request->query->all(). Commented Feb 17, 2015 at 14:07

2 Answers 2

3

Here's what I did:

{{ url('my_route', {myparam:'value'}|merge(app.request.query.all)) }} 
Sign up to request clarification or add additional context in comments.

2 Comments

That looks like a part of a twig file. Would you add that to every link in all your twig files?
Yes, this is part of my twig file and you'd need to add that to every twig file where you need that functionality. Alternatively you could write your own twig function for URLs generation as described here symfony.com/doc/current/cookbook/templating/twig_extension.html
0

You can do it like this:

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

3 Comments

And merging the new parameters everytime? Isn't there a method that brings that simple functionality?
@Mario my example is identical to array( 'q' => $request->get('q'), 'p' => $request->get('p') ), but easier..
I know. what i'm asking, is this the right / best solution? I hate repeating code, well that solution would force me to or writing a helper. But I think, i'm simply missing something, because I can't be the first one crossing this problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.