CookieCollections - markstory/cakephp GitHub Wiki
CakePHP 3.5.0 will feature a new set of cookie objects that provide an OO interface to building and using HTTP cookies. In addition to the Cookie object, we have a few different Cookie 'collections'. I'd like to unify those collections with the goal of only having 1 single cookie collection.
Use Cases for a CookieCollection
CookieCollections are used in a few different places in CakePHP:
- In an Http\Client as a cookie jar where state is retained between client requests.
- Adding cookies to a Controller Response.
- Reading cookie data from a ServerRequest.
Each use case requires some common methods and some use-case specific methods.
Generic Methods
- Iteration (IteratorAggregate)
has($key)
- Check if a cookie existsget($key)
- Get a cookieremove($key)
- Remove a cookieadd($cookie)
- Add a cookieall()
- Get all the cookies.
Methods specific to Http\Client usage
addFromResponse($response, $request)
- Add cookies from a response. A request is needed to infer the path and domain of cookies that have not included it.addToRequest($request)
- Apply all the cookies matching this request.
These two methods allow a CookieCollection to function as a 'cookie jar' for the client.
Methods specific to adding cookies to a Controller Response
addToResponse($response)
- Add cookies in the collection to a response.
Methods specific to Reading cookie data from a ServerRequest
createFromRequest($request)
- Build a new CookieCollection from a request object.
All of the above methods could be implemented in a single class allowing us to streamline how users have to interact with cookies in CakePHP even further.