Twig get url parameter with []

Twig get url parameter with []

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Twig get url parameter with []

When working with nested query parameters in a URL like MYURL?filter[_per_page]=25&filter[name][value]=hello, standard string extraction often fails because the data is structured as an associative array rather than a flat list. A common stumbling block occurs when attempting to retrieve these values directly within a Twig template using syntax like {{ app.request.get('filter[_per_page]') }}. This approach returns an empty string or null, which breaks client-side logic if you are trying to assign the result to a JavaScript variable.

The root cause lies in how PHP parses query strings before passing them to your application framework. When a URL contains brackets like filter[_per_page], the web server and PHP's native $_GET superglobal automatically interpret the first set of brackets as an array dimension. Consequently, the key becomes filter[0][_per_page]. The second bracket set in filter[name][value] creates a nested structure where name is the key under filter, and value holds the actual string.

To access these values correctly in Twig, you must account for this automatic indexing or utilize the framework's specific request object methods. In Laravel, which is widely used for such patterns, the Request helper provides a cleaner interface than accessing the raw $_GET array. You can use app('request')->get('filter._per_page', 25) to retrieve the value while automatically flattening the nested structure or handling the array index gracefully. Alternatively, request()->input('filter._per_page') is often more concise and explicitly handles null coalescing if a default value is needed.

If your goal is to pass this data to JavaScript for dynamic rendering, embedding it directly in an HTML attribute requires careful escaping. Using double curly braces inside a script tag, such as var param = "{{ app.request.get('filter[_per_page]') }}";, will not work as intended because the Twig engine renders the variable before the browser executes the script. Furthermore, accessing the key with brackets in the string literal itself does not resolve the underlying array flattening issue described earlier.

A robust solution involves ensuring that the value is retrieved using a dot notation key or a flattened accessor method compatible with your framework's request handling. For instance, if you are on Laravel 5.4 or later, request->get('filter._per_page') will return the integer 25. If the parameter is missing, providing a default ensures your JavaScript variable never receives an empty string, preventing potential syntax errors in subsequent calculations.

When constructing the final HTML for the client, consider using data attributes instead of inline script initialization for cleaner separation of concerns. You can output a hidden input or a data attribute that JavaScript reads:

<div data-filter-per-page="{{ app('request')->input('filter._per_page', 25) }}"></div>
<script>
    var param = document.querySelector('[data-filter-per-page]').dataset.filterPerPage;
</script>

This approach decouples the server-side retrieval logic from the client-side variable assignment. It also handles cases where the URL parameter is absent without crashing the application. Always remember that query parameters with brackets are technically arrays, and treating them as scalars requires explicit handling to avoid silent failures in your templates.

Our network