How to update a request CookieCollection after removing a cookie object

behavior

#1

Hello everyone, I am having problems working with request cookies on my project. I have a cookie collection with about 3 values a, b, and c.
Then I try the following for instance:
$cookieCollection = $this->getRequest()->getCookieCollection();
if ($cookieCollection->has('b')) {
$cookieCollection->remove('b');
}
After doing this the ‘b’ is removed only from this instance:
$cookieCollection.
But it is still there in $this->getRequest()->getCookieCollection();
Now how do I update the CookieCollection so that ‘b’ will no longer exist anywhere in the entire site?


#2

$this->request = $this->request->withCookieCollection($cookieCollection);


#3

Thanks ADmad, for your response. But I have tried that, and it’s not working.


#4

Both the request and the cookie collection are immutable objects, so in addition to what @ADmad said, you also need

$cookieCollection = $cookieCollection->remove('b');