This component is designed to ease URL handling.
The second method argument ($url
) can be one of these possible types:
- If the value is
null
, the current URL is used (\Contao\Environment::get('requestUri')
). - If the value is a
string
, it's assumed to be a valid URL and used directly.
Adding a query string is as simple as passing it as a string argument. Haste will correctly add an ampersand or question mark to the URL as needed.
$this->urlParser->addQueryString('foo=bar');
You can also add multiple query strings with ease:
$this->urlParser->addQueryString('foo1=bar1&foo2=bar2');
The removeQueryString
method accepts an array of query parameters to remove.
$this->urlParser->removeQueryString(['foo', 'bar']);
For more complex needs, query parameters can be removed using a callback.
The callback must return boolean true to keep the query parameters.
This method behaves similar to array_filter
with flag ARRAY_FILTER_USE_BOTH
.
// Remove keys that start with "foo_"
$this->urlParser->removeQueryStringCallback(static fn ($value, $key) => str_contains($key, 'foo_'));