I need to re-generate the URL of my page, removing the additional parameters. For example: when I receive:
/bao1/bao2/?removeMe1=anything&keepMe1=anything&removeMe2=&keepMe2=anything
I want to generate the URL with removeMe
query var removed, but with everything else intact. Like this:
/bao1/bao2/?keepMe1=anything&keepMe2=anything
I autowired the request:
public function __construct(RequestStack $httpRequest)
{
$this->httpRequest = $httpRequest;
}
Then I'm playing around like this:
public function getCleanUrl()
{
// HttpFoundation\Request
$currentHttpRequest = $this->httpRequest->getCurrentRequest();
// Trying to remove the parameters
$currentHttpRequest->query->remove("removeMe1");
return $currentHttpRequest->getUri()
}
The query->remove("removeMe1")
works, but when I invoke getUri()
I still get the full input url, as if remove()
was never invoked. I think I'm probably missing to call some kind of $currentHttpRequest->regenerate()->getUri()
but I cannot find anything.
To get the modified URL after calling mutator methods on a
Request
object, you need to calloverrideGlobals()
.If not,
Request
methods will give you results accordin to the original superglobals ($_GET
,$_POST
,$_SERVER
). By callingRequest::overrideGlobals()
you tell the object not to.E.g.:
Or maybe, something more adjusted to your use case (untested, but the general idea should hold):