Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 / Twig dealing with getting URL query string values

Tags:

twig

symfony

I'm trying to manipulate the query string values in a URL.

I can get the current URL or route either from the Request object or Twig's functions, but it's the query string I'm struggling with.

I don't need app.request.attributes.get('_route_params') as this gets the query string params that are in the route.

I need to get query string params that are actually in the URL.

I want to be able to do the two things listed below in both Symfony2 (in a PHP controller) and Twig (in a Twig template):

  1. Get all current query string values in the URL and display them

  2. Do 1, but change one of the query string values before displaying them

I can't find anyone who knows how to do this.

like image 309
user2143356 Avatar asked Mar 25 '13 01:03

user2143356


People also ask

How do I get the current URL in twig?

You can get the current URL in Twig/Silex 2 like this: global. request. attributes. get('_route') .

How can I tell if a URL has a query string?

To check if a url has query parameters, call the includes() method on the string, passing it a question mark as a parameter, e.g. str. includes('? ') . If the url contains query parameters, the includes method will return true , otherwise false is returned.

What are query string parameters in URL?

What are query string parameters? Query string parameters are extensions of a website's base Uniform Resource Locator (URL) loaded by a web browser or client application. Originally query strings were used to record the content of an HTML form or web form on a given page.


1 Answers

You can use app.request.query.all to get your query strings.

If you want to change a param in Twig you can do this

{% set queryParams = app.request.query.all %} {% set queryParams = queryParams|merge({queryKey: newQueryValue}) %} 
like image 116
Thomas Potaire Avatar answered Sep 24 '22 05:09

Thomas Potaire