<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>cakebaker &#187; cookie</title>
	<atom:link href="http://cakebaker.42dh.com/tags/cookie/feed/" rel="self" type="application/rss+xml" />
	<link>http://cakebaker.42dh.com</link>
	<description>baking cakes with CakePHP</description>
	<lastBuildDate>Tue, 20 Dec 2011 15:29:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Let&#8217;s eat cake and cookies</title>
		<link>http://cakebaker.42dh.com/2007/01/20/lets-eat-cake-and-cookies/</link>
		<comments>http://cakebaker.42dh.com/2007/01/20/lets-eat-cake-and-cookies/#comments</comments>
		<pubDate>Sat, 20 Jan 2007 10:41:33 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[component]]></category>
		<category><![CDATA[cookie]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/2007/01/20/lets-eat-cake-and-cookies/</guid>
		<description><![CDATA[One of the new components coming with CakePHP 1.2 is the cookie component. It is a rather simple component, so it is very easy to use this component. Let us do an example. As always when using a component we have to add the cookie component to the $components array of our controller: var $components [...]]]></description>
			<content:encoded><![CDATA[<p>One of the new components coming with CakePHP 1.2 is the cookie component. It is a rather simple component, so it is very easy to use this component. Let us do an example.</p>
<p>As always when using a component we have to add the cookie component to the $components array of our controller:</p>
<pre>
var $components = array('Cookie');
</pre>
<p>If we now call our controller in the browser we will see the following notices (if we are in the debug modus):</p>
<pre>
Notice: For added security you should add the var cookieName to your Controller or AppController in
/home/dho/projects/cake_1.2.x.x/cake/libs/controller/components/cookie.php on line 153

Notice: For added security you should add the var cookieKey to your Controller or AppController in
/home/dho/projects/cake_1.2.x.x/cake/libs/controller/components/cookie.php on line 161

Notice: Add var cookieDomain = .yourdomain.com; to your Controller or AppController to allow access on all subdomains in
/home/dho/projects/cake_1.2.x.x/cake/libs/controller/components/cookie.php on line 169
</pre>
<p>To remove these notices we do what is recommended in the notices and add the expected variables to our controller:</p>
<pre>
var $cookieName = 'myCookie';
var $cookieKey = 'has82js737hak2';
var $cookieDomain = '.myproject.localhost';
</pre>
<p>As it is not very cool to add a development domain to the code, we will refactor a bit and set the domain dynamically in the constructor:</p>
<pre>
var $cookieDomain;

function __construct() {
    parent::__construct();
    $this-&gt;cookieDomain = '.'.$_SERVER['SERVER_NAME'];
}
</pre>
<p>Writing to the cookie and reading from it is as you would expect:</p>
<pre>
$this-&gt;Cookie-&gt;write('User.id', 1);
</pre>
<p>respectively</p>
<pre>
$this-&gt;Cookie-&gt;read('User.id');
</pre>
<p>For more details see the <a href="http://api.cakephp.org/1.2/class_cookie_component.html">API</a> <del datetime="2007-04-13T15:10:18+00:00">(thanks to Christian Winther aka Jippi for providing these API docs)</del>.</p>
<p>P.S.: For those of you who are using my unofficial test suite there is a <a href="http://cakebaker.42dh.com/downloads">new version</a> available which supports the cookie component. You can use it in your tests as shown in the following test function which tests that a user with a cookie is automatically logged in when requesting /users/login:</p>
<pre>
function testLoginWithCookie() {
    $this-&gt;get('/users/login', null, array('User.id' =&gt; $this-&gt;dho['id']));
    $this-&gt;assertRedirectedTo('/projects');
}
</pre>
<p>You can also check if the correct value has been written to the cookie with something like:</p>
<pre>
$this-&gt;assertEqual($this-&gt;dho['id'], $this-&gt;cookie['User.id']);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2007/01/20/lets-eat-cake-and-cookies/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>

