nystudio107 / craft-cookies

A simple plugin for setting and getting cookies from within Craft CMS templates.

Home Page:https://nystudio107.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Removing cookies throws an error

riasvdv opened this issue · comments

commented

When removing a cookie, Craft throws the following error

afbeelding

When checking if you have to remove the cookie, there's the following code:

if ($name == "") {
    Craft::$app->request->cookies->remove($name);
}

Shouldn't this be if ($value == "")?

But if I change that the following error is thrown:

afbeelding

If I change the whole statement to this:

if ($value == "") {
    Craft::$app->request->cookies->readOnly = false;
    Craft::$app->request->cookies->remove($name);
    Craft::$app->request->cookies->readOnly = true;
}

It works, but that seems a bit convulted somehow?

commented

Ah, dove into Yii2 and it seems that when you remove cookies, you have to do it from the response class and not the request, so like this:

Craft::$app->response->cookies->remove($name);

commented

Created a PR #2 that should fix everything :)

Thanks @Rias500 !

I've also added try/catch so errors are logged instead of exceptions thrown