From e743fedc65c653c78fac363e03914ea4c2a25c99 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Sat, 15 Nov 2014 12:58:31 +1300 Subject: [PATCH] Importing cookie content --- .../18_Cookies_And_Sessions/01_Cookies.md | 65 ++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/01_Cookies.md b/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/01_Cookies.md index 32ae7cea1..8c589c887 100644 --- a/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/01_Cookies.md +++ b/docs/en/02_Developer_Guides/18_Cookies_And_Sessions/01_Cookies.md @@ -4,6 +4,7 @@ summary: A set of static methods for manipulating PHP cookies. # Cookies Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. + SilverStripe uses cookies for remembering users preferences. Application code can modify a users cookies through the [api:Cookie] class. This class mostly follows the PHP API. @@ -35,6 +36,68 @@ Clears a given cookie. // Cookie::force_expiry('MyApplicationPreference') + +## Cookie_Backend + +The [api:Cookie] class manipulates and sets cookies using a [api:Cookie_Backend]. The backend is in charge of the logic +that fetches, sets and expires cookies. By default we use a [api:CookieJar] backend which uses PHP's +[setcookie](http://www.php.net/manual/en/function.setcookie.php) function. + +The [api:CookieJar] keeps track of cookies that have been set by the current process as well as those that were received +from the browser. + + :::php + $myCookies = array( + 'cookie1' => 'value1', + ); + + $newBackend = new CookieJar($myCookies); + + Injector::inst()->registerService($newBackend, 'Cookie_Backend'); + + Cookie::get('cookie1'); + +## Resetting the Cookie_Backend state + +Assuming that your application hasn't messed around with the `$_COOKIE` superglobal, you can reset the state of your +`Cookie_Backend` by simply unregistering the `CookieJar` service with `Injector`. Next time you access `Cookie` it'll +create a new service for you using the `$_COOKIE` superglobal. + + :::php + Injector::inst()->unregisterNamedObject('Cookie_Backend'); + + Cookie::get('cookiename'); // will return $_COOKIE['cookiename'] if set + + +Alternatively, if you know that the superglobal has been changed (or you aren't sure it hasn't) you can attempt to use +the current `CookieJar` service to tell you what it was like when it was registered. + + :::php + //store the cookies that were loaded into the `CookieJar` + $recievedCookie = Cookie::get_inst()->getAll(false); + + //set a new `CookieJar` + Injector::inst()->registerService(new CookieJar($recievedCookie), 'CookieJar'); + + +### Using your own Cookie_Backend + +If you need to implement your own Cookie_Backend you can use the injector system to force a different class to be used. + + :::yml + --- + Name: mycookie + After: '#cookie' + --- + Injector: + Cookie_Backend: + class: MyCookieJar + +To be a valid backend your class must implement the [api:Cookie_Backend] interface. + + ## API Documentation -* [api:Cookie] \ No newline at end of file +* [api:Cookie] +* [api:CookieJar] +* [api:CookieBackend] \ No newline at end of file