Hint on using Cookies?

Am I that dense?

I cannot for the life of me figure out from the docs how to set and read a cookie under 3.9.

There’s a menu item on the right: ‘Cookies’ — that goes nowhere. Searching the page for references to "Cookie’ I find not a single mention of how to read a cookie unless it’s in a cookie collection. I see two different places telling me how to set cookies in three different ways. None of which happen to work as far as I can tell. No cookie of any name ever gets set. No mention, incidentally, that you must Use DateTime (or else just new \DateTime() ).

I find no discussion about why I might want to use a cookie collection, nor do I find anything telling me how to invoke a cookie collection for the sole purpose of checking if one particular cookie has already been set.

The use case is common and dead simple: developer wants to check if a cookie has been set with a given value, and, if not, take a specific action based on that result, likely including setting said cookie at some point.

But the “CakePHP Way™” confounds this otherwise trivial task into an abstract multi-lines of code task and the docs don’t bother to justify how or why this abstraction has been implemented… worse: the docs utterly fail to lucidly explain how to leverage this black box.

Why should I bother trying to get the CakePHP Way™ to work when I can just use native PHP for my cookie management and be done in 1/1000th of the time it takes me to go through the docs, google searches, and post here seeking help?

I’m going to beat that dead horse: With every version, CakePHP reinvents how to do things differently and can’t even keep its own system consistent or docs properly updated. Cookies used to be handled a different way, and apparently, under 4.x they are handled in yet another different way compared to 3.x (and even 3.9 which is different from earlier 3.x).

It’s a cookie! This isn’t some single-signon system. It’s fundamental basic tech that has been around for decades — and I’m failing to get it working under a modern framework. And mostly it’s because the doc writers just couldn’t be bothered to spend time on this fundamental feature. This should be teachable in no more than two paragraphs worth of space on the Request/Response page with two cogent examples.

:man_facepalming:t4:

Reading cookies looks to me to be the same, simple interface as provided in 3.9. And here is how to set them.

I did eventually manage to get a cookie to be set.

However, I still can’t read it without dropping to $_COOKIE for the task.

Did you follow the link? I’m not using cookies (except automatically through the auth stuff), but it looks pretty straight-forward.

I missed the first link but just looked it over now. It’s actually the exact same thing as 3.9… the menu item that says “cookies” links nowhere.

The only part that says how to read cookies requires setting up a CookieCollection… which I don’t know why I would want to do that. Seems like even more overhead.

Regardless, I did try that approach yesterday, but failed. The issue is that you need a cookie object to read the cookie but I don’t see any instructions on how to create a new cookie object without doing it for the purpose of creating a cookie. Which is a whole chicken-egg thing when the very first thing you want to do is find out if the user has the cookie before attempting to do anything.

It’s above my pay grade. :rofl:

$this->request->getCookie('remember_me'); does not require a collection. It assumes you’re in a controller, where $this->request is a thing you can access, but if you’re not there’s other ways to get to the request object.

1 Like

Wow. Thank you!

I never saw that documented. Went back and checked and it’s not on the request/response page (either for 3 or 4).

I tried doing a search on the docs for getCookie and the search preview shows your example, but when you click on the link, it’s not actually on that page at all.

So I guess someone inadvertently deleted it from the docs.

It’s the very first example on the first link that I sent…

I don’t see it…?

Are we looking at the same page?

Screenshot 2021-02-05 143514

I don’t know what to say. That is literally and absolutely NOT on the version of the page I am seeing.

To be extra clear: there is NO #cookies anchor present on the page I see. Even when I click the link in the sidebar, it goes nowhere. When I use command-F in my browser for “getCookie” no results are returned. When I use command-F in my browser for “cookie” nothing of the sort appears anywhere after a full command-G cycle.

On further investigation, I viewed source on the page and did find the relevant section (and corresponding anchor).

So why can’t I see it? I tried Inspect Element on the item right prior and then looked at the section id=“cookies” and noticed it has display:none set. — User Agent Stylesheet.
.
So there’s the explanation. Safari (at least latest version) for Mac, is blocking anything with an id of ‘cookies’ apparently. This may be because the page also has Facebook trackers and Google Analytics trackers on it.

I’ll contact Apple about it. But I suspect they’ll hold tight to their hard line against trackers and this being necessary etc. There may be a workaround for the Cake site (and anyone else running into similar issues).

The documentation is all in a repo that you can do a pull request on. Maybe “request-cookies” would be a suitable anchor that works around that.

1 Like

Further investigation revealed the issue is caused by AdGuard, not Safari.

I filed a bug report (worse, it persists even after I disabled the extension for book.cakephp.org)