<?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>DerEuroMark</title>
	<atom:link href="http://www.dereuromark.de/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dereuromark.de</link>
	<description>A blog about Frameworks (CakePHP), MVC, Snippets, Tips and more</description>
	<lastBuildDate>Sat, 04 Feb 2012 20:42:37 +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>More persistent sessions in Cake2.x</title>
		<link>http://www.dereuromark.de/2012/02/02/more-persistent-sessions-in-cake2-x/</link>
		<comments>http://www.dereuromark.de/2012/02/02/more-persistent-sessions-in-cake2-x/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 18:05:31 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Common]]></category>
		<category><![CDATA[Auth]]></category>
		<category><![CDATA[Authentication]]></category>
		<category><![CDATA[AutoLogin]]></category>
		<category><![CDATA[cake2.x]]></category>
		<category><![CDATA[Login]]></category>
		<category><![CDATA[PHP5]]></category>
		<category><![CDATA[Session]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1489</guid>
		<description><![CDATA[This has been an issue for me way too long. I always hated the fact that users are logged out almost every few hours. Compared to other sites on the www it was ridiculously short, no matter how long you set timeout and cookieTimeout in core settings. I looked into it more than twice in [...]]]></description>
			<content:encoded><![CDATA[<p>This has been an issue for me way too long.<br />
I always hated the fact that users are logged out almost every few hours. Compared to other sites on the www it was ridiculously short, no matter how long you set timeout and cookieTimeout in core settings.</p>
<p>I looked into it more than twice in the last year coming up with almost nothing. After many hours of investigating I just postponed it again for times unknown.<br />
It should be said that debugging the session/cookie based stuff &#8211; here Authentication &#8211; is quite a difficult past. Many things play into it making it hard to reproduce the issue.</p>
<p>The cake documentation also doesn&#8217;t tell us, that for php sessions to work this long it is also required to raise the internal max_lifetime. I stumpled across this by accident. Always thought this would be taken care of by cake itself.<br />
But after switching to database as session container I didn&#8217;t notice any improvements either. So the server side garbage collector maybe wasn&#8217;t responsible after all. At least not all alone.</p>
<p>So, to sum it up:<br />
- Setting up timeouts was a waste of time<br />
- Security level, as well (I just left it at `low`)<br />
- Moving from php/cake to database didn&#8217;t work either &#8211; although it provides little more control<br />
- Raising the php.ini settings for gc_maxlifetime didn&#8217;t work too well, either</p>
<p>I still get logged out after totally different lengths. Sometimes minutes, sometimes a few hours. It never lasts much longer.<br />
Not only a deadly problem for a social network running with Cake.</p>
<p>I tried multiple times to add an encrypted cookie as `RememberMe` functionally (to quicklogin such a logged out user again). But I failed &#8211; as I later found out due to a PHP bug with srand/mt_srand and suhosin (<a href="http://www.adayinthelifeof.nl/2010/12/13/php-srand-problems-with-suhosin/">details</a>).<br />
Thanks to <a href="http://milesj.me/blog/read/security-cipher-suhosin">miles&#8217; post regarding this bug</a> I took a step forward and decided to take action (a lot of users have been complaining about unexplainable logouts the past months anyway).</p>
<h3>AutoLogin? A way out?</h3>
<p>I finally &#8211; after putting up with it for many years &#8211; adjusted Miles&#8217; great spadework with his <a href="http://milesj.me/code/cakephp/auto-login">AutoLogin component</a> to work with my apps.</p>
<p>Basically, this component saves the user data to a cookie on login. As soon as the session gets lost again (for whatever reason!?) the component re-logins the user. It happens silently without bothering the user.</p>
<p>The code can be found here: <a href="https://github.com/dereuromark/cake-auto_login">github.com/dereuromark/cake-auto_login</a></p>
<p>I actually merged it into my Tools plugin. But for the sake of simplicity I left the forked rep from Miles intact and up-to-data.</p>
<h3>Usage and Features</h3>
<p>After successfully running it in a few cake2.x apps, I want to share the basic HOWTO.<br />
The only required thing to do is adding the component globally in AppController:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$components</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Session'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'RequestHandler'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AutoLogin'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Auth'</span><span style="color: #339933;">,</span> <span style="color: #339933;">...</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>It is important to include it before any AuthComponent to avoid getting any &#8220;not logged in&#8221; error messages triggered by it.</p>
<p>You can use /Config/configs.php (or any other Configure place) to define its settings:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'AutoLogin'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'account'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'login'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'requirePrompt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span>
	<span style="color: #339933;">...</span> <span style="color: #666666; font-style: italic;">//see the component for details on other options</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>Please use the last one with caution. If `requirePrompt` is disabled you use AutoLogin for ALL logins. This requires users to always log out correctly in public places (especially in internet cafes this can be quite dangerous as others opening the site could hijack this logged in user even after several days). So make sure you use is only for sites where all users are properly informed and educated on this.</p>
<p>So for the default case you will need to add a checkbox into your login form:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Configure<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AutoLogin.active'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'auto_login'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'checkbox'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'label'</span><span style="color: #339933;">=&gt;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Remember on this computer'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>That&#8217;s it.<br />
You can easily test it using database sessions and truncating the session table after login.<br />
It should log the user right back in as well as creating the new session table row. If everything is working right, the user shouldn&#8217;t even notice that he wasn&#8217;t logged in for a split second.</p>
<p>The if statement is optional. I use it to dynamically enable/disable the component based on the environment.<br />
But if you use it, make sure you defined `active` as `true` in Configure.</p>
<p><u>Getting rid of the suhosin bug</u><br />
As explained by Miles in the above links, most linux apache environments come with the suhosin patch which messes up srand by default. My localhost WAMP doesn&#8217;t. So there it worked right away. But to get it to work on the linux environment, all there is to do is:<br />
In your `/etc/php5/apache2/php.ini` add this line at the bottom:</p>
<pre class="php" style="font-family:monospace;">suhosin<span style="color: #339933;">.</span><a href="http://www.php.net/srand"><span style="color: #990000;">srand</span></a><span style="color: #339933;">.</span>ignore <span style="color: #339933;">=</span> Off</pre>
<p>And don&#8217;t forget to restart apache or at least `/etc/init.d/apache2 force-reload`.</p>
<p>TIP: I added a test case for this bug (see test file in the rep). Run it on every environment if you are not sure whether it is affected or not.</p>
<p><u>More tips</u><br />
If you want to disable AutoLogin for specific sites you can simply set `active` to `false` for this site in your configs or dynamically in the controllers&#8217; constructor.<br />
Remember: Since you need to include it before Auth etc you cannot dynamically add it to the list of components. But you can always disable the included component this way <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The debug mode is on auto-detect by default. If you develop locally (debug > 0) you have it enabled right away. But, of course, you can overwrite this value in your configs.</p>
<p>Another important setting is `expires` &#8211; it defaults to `2 weeks` but can also easily be adjusted.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1489&amp;md5=294a09e1a4e763e85afa61b5a7255edd" title="Flattr" target="_blank"><img src="http://www.dereuromark.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2012/02/02/more-persistent-sessions-in-cake2-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1489&amp;md5=294a09e1a4e763e85afa61b5a7255edd" type="text/html" />
	</item>
		<item>
		<title>New year, new cake version &#8211; 2.1</title>
		<link>http://www.dereuromark.de/2012/01/09/new-year-new-cake-version-2-1/</link>
		<comments>http://www.dereuromark.de/2012/01/09/new-year-new-cake-version-2-1/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 22:08:52 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[cake2.0]]></category>
		<category><![CDATA[cake2.1]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1473</guid>
		<description><![CDATA[The new version 2.1 is almost fully backwards compatible. The migration guide from 2.0 to 2.1 can be found at book.cakephp.org/2.0/en/appendices/2-1-migration-guide.html New stuff for the upgrade shell I used this chance to enhance my version of the upgrade shell and added &#8211; besides some minor fixes &#8211; a command `cake21`. It should take care of [...]]]></description>
			<content:encoded><![CDATA[<p>The new version 2.1 is almost fully backwards compatible.</p>
<p>The migration guide from 2.0 to 2.1 can be found at<br />
<a href="http://book.cakephp.org/2.0/en/appendices/2-1-migration-guide.html">book.cakephp.org/2.0/en/appendices/2-1-migration-guide.html</a></p>
<h3>New stuff for the upgrade shell</h3>
<p>I used this chance to enhance my version of the upgrade shell and added &#8211; besides some minor fixes &#8211; a command `cake21`. It should take care of all api standard updates for 2.1: <a href="https://github.com/dereuromark/upgrade/commit/5d889694238936a2f6b570c9dbfe665bbdaf6790#L1L218">git commit1</a> and <a href="https://github.com/dereuromark/upgrade/commit/1738d72594c4d1de1fe8b0958392a3d43aa4d4f1">2</a><br />
The Upgrade Plugin can be found <a href="https://github.com/dereuromark/upgrade">here</a>.</p>
<p>It currently updates Auth::allow() and layout stuff ($content_for_layout, $scripts_for_layout etc).</p>
<p>Feel free to add missing replacements.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1473&amp;md5=d09f42ca0d8599f1768c38f495114b11" title="Flattr" target="_blank"><img src="http://www.dereuromark.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2012/01/09/new-year-new-cake-version-2-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1473&amp;md5=d09f42ca0d8599f1768c38f495114b11" type="text/html" />
	</item>
		<item>
		<title>Traits &#8211; PHP5.4</title>
		<link>http://www.dereuromark.de/2011/12/31/traits-php5-4/</link>
		<comments>http://www.dereuromark.de/2011/12/31/traits-php5-4/#comments</comments>
		<pubDate>Sat, 31 Dec 2011 10:00:48 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP5.4]]></category>
		<category><![CDATA[Traits]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1451</guid>
		<description><![CDATA[Since it is new years eve, I don&#8217;t want to start a complete new chapter. So I will write one last post this year with some more general topic. And yes &#8211; I find it so interesting that I have to mention it in an own post Traits With PHP5.4 approaching fast, it will probably [...]]]></description>
			<content:encoded><![CDATA[<p>Since it is new years eve, I don&#8217;t want to start a complete new chapter.<br />
So I will write one last post this year with some more general topic.<br />
And yes &#8211; I find it so interesting that I have to mention it in an own post <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Traits</h3>
<p>With PHP5.4 approaching fast, it will probably be the standard sometime in 2012.<br />
It will have one major feature that we will all going to love: <a href="http://www.php.net/manual/en/language.oop5.traits.php" title="php.net/manual/en/language.oop5.traits.php">oop5.traits</a>.</p>
<p>What are they?<br />
The above link describes it pretty good. They are some kind of behavior &#8211; like the one we attach to models right now. Only way more flexible and powerful.<br />
Every class can then use those `behaviors`.</p>
<pre class="php" style="font-family:monospace;">trait Hello <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello '</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
trait World <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sayWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'World!'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
trait HelloWorld <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">use</span> Hello<span style="color: #339933;">,</span> World<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyHelloWorld <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">use</span> HelloWorld<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$Foo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyHelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sayHello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sayWorld</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>The result would be <code>Hello World!</code></p>
<p>Imagine what would be possible with Controllers, Components, Shells, and other classes that can now not only extend parent classes but also use traits.</p>
<h3>Cool things you can do with them</h3>
<p>The above link contains a good set of examples.<br />
I will outline the most interesting ones:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sayWhere<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// same with __FILE__ etc</span>
<span style="color: #009900;">&#125;</span></pre>
<p>As of right now objects that are extended by some subclass would always return the wrong class, because inside a class those magic constants always relate to the current file (and therefore often the parent class defining it).<br />
With traits the magic is endless &#8211; they will make it possible to inject functionality that overcomes that deficiency:</p>
<pre class="php" style="font-family:monospace;">trait sayWhere <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> whereAmI<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Hello <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">use</span> sayWHere<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> World <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">use</span> sayWHere<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$One</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Hello<span style="color: #339933;">;</span>
<span style="color: #000088;">$One</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">whereAmI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Hello</span>
&nbsp;
<span style="color: #000088;">$Two</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> World<span style="color: #339933;">;</span>
<span style="color: #000088;">$Two</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">whereAmI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//World</span></pre>
<p>Another neat feature is, that we can override specific methods if needed &#8211; both ways:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Base <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">use</span> SayBye<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// we want this method to be overridden by the trait to do some extra stuff with it</span>
	<span style="color: #666666; font-style: italic;">// can only be done in the inheriting class, though</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello '</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
trait SayWorld <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		parent<span style="color: #339933;">::</span><span style="color: #004000;">sayHello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'World!'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
trait SayBye <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// this method will be be overridden by the class method to do some extra stuff with it</span>
	<span style="color: #666666; font-style: italic;">// can only be done in the inheriting class, though</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sayBye<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Bye '</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyHelloWorld <span style="color: #000000; font-weight: bold;">extends</span> Base <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">use</span> SayWorld<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sayBye<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">sayBye</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'World!'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$Foo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyHelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sayHello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Hello World!</span>
<span style="color: #000088;">$Foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sayBye</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Bye World!</span></pre>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1451&amp;md5=103ece3fadfb8e905ddd4f4c6b342ae1" title="Flattr" target="_blank"><img src="http://www.dereuromark.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2011/12/31/traits-php5-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1451&amp;md5=103ece3fadfb8e905ddd4f4c6b342ae1" type="text/html" />
	</item>
		<item>
		<title>TinyAuth &#8211; The fastest and easiest authorization for Cake2</title>
		<link>http://www.dereuromark.de/2011/12/18/tinyauth-the-fastest-and-easiest-authorization-for-cake2/</link>
		<comments>http://www.dereuromark.de/2011/12/18/tinyauth-the-fastest-and-easiest-authorization-for-cake2/#comments</comments>
		<pubDate>Sun, 18 Dec 2011 20:24:19 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Access]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[Auth]]></category>
		<category><![CDATA[Authentication]]></category>
		<category><![CDATA[Authorization]]></category>
		<category><![CDATA[cake2.0]]></category>
		<category><![CDATA[Login]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1423</guid>
		<description><![CDATA[The cake built in CRUD auth is way too powerful and way too slow and memory consuming. In 99% of all cases there is no need for that. If you just want to have some basic control over the access to specific actions, you need sth else. Here it comes. Preparations I assume you already [...]]]></description>
			<content:encoded><![CDATA[<p>The cake built in CRUD auth is way too powerful and way too slow and memory consuming.<br />
In 99% of all cases there is no need for that.<br />
If you just want to have some basic control over the access to specific actions, you need sth else.<br />
Here it comes.</p>
<h3>Preparations</h3>
<p>I assume you already got the AuthComponent included in the $components array of your AppController.<br />
You probably also excluded all public views with sth like</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allow</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contact_form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># in beforeFilter() of the specific controllers</span></pre>
<p>This here (in the contact controller) makes Auth skip this action completely. The action will be accessable to everybody right away.</p>
<p>You probably got a Role model (User belongsTo Role / User hasAndBelongsToMany Role) attached to the User.<br />
If you don&#8217;t want this, use Configure to store your keys like so:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Role'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'superadmin'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'admin'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'moderator'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'helper'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'user'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>You should at least have the <code>user</code> and <code>admin</code> role for it to make sense.</p>
<p>You should also have some kind of Authentication in your AppController:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticate</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Form'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># uses username and password for login</span></pre>
<p>So far so good. You can login/logout and once you are logged in browse all non-public pages.<br />
Even admin pages, of course. Thats where the TinyAuth class comes in.</p>
<h3>TinyAuth</h3>
<p>The code can be found at <a href="https://github.com/dereuromark/tools/blob/2.0/Controller/Component/Auth/TinyAuthorize.php">github</a>.</p>
<p>First of all include it in your beforeFilter() method of the AppController:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authorize</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tools.Tiny'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>If you don&#8217;t place this code into your &#8220;Tools&#8221; plugin, make sure to remove the Tools. prefix then.</p>
<p>Now create a file in /Config/ called acl.ini like so:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>Tools<span style="color: #339933;">.</span>Countries<span style="color: #009900;">&#93;</span>
<span style="color: #339933;">*</span> <span style="color: #339933;">=</span> superadmin <span style="color: #339933;">;</span> this is a comment
&nbsp;
<span style="color: #009900;">&#91;</span>Account<span style="color: #009900;">&#93;</span>
edit<span style="color: #339933;">,</span>change_pw <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>
&nbsp;
<span style="color: #009900;">&#91;</span>Activities<span style="color: #009900;">&#93;</span>
admin_index<span style="color: #339933;">,</span>admin_edit<span style="color: #339933;">,</span>admin_add<span style="color: #339933;">,</span>admin_delete <span style="color: #339933;">=</span> admin<span style="color: #339933;">,</span>superadmin
index <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>
&nbsp;
<span style="color: #009900;">&#91;</span>Users<span style="color: #009900;">&#93;</span>
index<span style="color: #339933;">,</span>search <span style="color: #339933;">=</span> user
<span style="color: #339933;">*</span> <span style="color: #339933;">=</span> moderator<span style="color: #339933;">,</span>admin</pre>
<p>The format is normal PHP INI style. I already included all kind of examples. * is a placeholder for &#8220;any&#8221;.<br />
The plugin prefix for controllers is not necessary as of now (maybe for Cake3 where the same controller name is allowed multiple times due to PHP5.3 namespaces).<br />
Comments in ini files start with &#8220;;&#8221;.</p>
<p>Explanations:<br />
- Superadmin can access all Countries actions<br />
- Account actions are accessable by all roles (and therefore logged in users)<br />
- Activities can be modified by all admins and listed by all everyone<br />
- Users can search and list other others, but only moderators and admins have access to all other ations</p>
<p>That&#8217;s it. Really easy, isn&#8217;t it?</p>
<h3>Some details</h3>
<p>TinyAuth expects a Session Auth User like so:</p>
<pre class="php" style="font-family:monospace;">Auth<span style="color: #339933;">.</span>User<span style="color: #339933;">.</span>id
Auth<span style="color: #339933;">.</span>User<span style="color: #339933;">.</span>role_id <span style="color: #009900;">&#40;</span>belongsTo <span style="color: #339933;">-</span> role <a href="http://www.php.net/key"><span style="color: #990000;">key</span></a> directly in the users table<span style="color: #009900;">&#41;</span></pre>
<p>or so:</p>
<pre class="php" style="font-family:monospace;">Auth<span style="color: #339933;">.</span>User<span style="color: #339933;">.</span>id
Auth<span style="color: #339933;">.</span>User<span style="color: #339933;">.</span>Role <span style="color: #009900;">&#40;</span>hasAndBelongsToMany <span style="color: #339933;">-</span> multi role <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> containing all role keys<span style="color: #009900;">&#41;</span></pre>
<p>As you can see <strong>it can manage both single and multile role setup</strong>.<br />
That&#8217;s sth the core one lacks, as well.</p>
<p>The current configuration is cached in the persistent folder by default. In development mode (debug > 0) it will be regenerated all the time, though. So remember that you have to manually clear your cache in productive mode for changes to take effect!</p>
<h3>Quicktips</h3>
<p>If you have a cleanly separated user/admin interface there is a way to allow all user actions to users right away;</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authorize</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tools.Tiny'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'allowUser'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>Only for admin views the authorization is required then.</p>
<p>If you got a &#8220;superadmin&#8221; role and want it to access everything automatically, do this in the beforeFilter method of your AppController:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$userRoles</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Auth.User.Role'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$userRoles</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span>Configure<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Role.superadmin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$userRoles</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;"># Skip auth for this user entirely
</span>	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allow</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'*'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<h3>UPDATE 2012-01-10</h3>
<p>The auth model can now be anything you like. It doesn&#8217;t have to be `Role` or `role_id`.<br />
The new cake2.x uses &#8220;groups&#8221; per default.<br />
You can easily adjust that now by passing <code>`aclModel` => 'Group'</code> or <code>`aclKey` => 'group_id'</code> to the Tiny class, for instance.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1423&amp;md5=6ef24e2d6f47b307c91e9c48a6060d11" title="Flattr" target="_blank"><img src="http://www.dereuromark.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2011/12/18/tinyauth-the-fastest-and-easiest-authorization-for-cake2/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<atom:link rel="payment" href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1423&amp;md5=6ef24e2d6f47b307c91e9c48a6060d11" type="text/html" />
	</item>
		<item>
		<title>Tools Plugin – Part 2: Contact Form</title>
		<link>http://www.dereuromark.de/2011/12/15/tools-plugin-part-2-contact-form/</link>
		<comments>http://www.dereuromark.de/2011/12/15/tools-plugin-part-2-contact-form/#comments</comments>
		<pubDate>Thu, 15 Dec 2011 19:27:55 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=76</guid>
		<description><![CDATA[I want to show how easy it is to make a solid and universal contact form in Cake(2). The full code can be found in the github rep of the Tools plugin. Model The most important part first: We need a solid validation for the form. How many forms are out there that do not [...]]]></description>
			<content:encoded><![CDATA[<p>I want to show how easy it is to make a solid and universal contact form in Cake(2).<br />
The full code can be found in the github rep of the <a href="https://github.com/dereuromark/tools/blob/2.0/Model/ContactForm.php">Tools plugin</a>.</p>
<h3>Model</h3>
<p>The most important part first: We need a solid validation for the form. How many forms are out there that do not have any validation. The first thing I always do on about any public contact form: Hit the submit button and smile it if tells me that my empty form &#8220;has just been successfully sent&#8221;. We can do better than that <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ContactForm <span style="color: #000000; font-weight: bold;">extends</span> ToolsAppModel <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_schema</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'string'</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">'null'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'30'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'email'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'string'</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">'null'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'60'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'subject'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'string'</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">'null'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'60'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'text'</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">'null'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$useTable</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$validate</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'notEmpty'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'rule'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'notEmpty'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'valErrMandatoryField'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'last'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
			<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #339933;">...</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>The _schema var mocks a database table so that we don&#8217;t really need one. This helps the FormHelper to generate the inputs (maxlength, type, &#8230;).<br />
The validation rules will make sure the email is valid and the user actually entered some text.</p>
<h3>Controller</h3>
<p>The logic cakes care of the validation on POST.<br />
<code>/Controller/ContactController.php</code></p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> ContactController <span style="color: #000000; font-weight: bold;">extends</span> AppController <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$uses</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tools.ContactForm'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'put'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Auth.User.id'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ContactForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Behaviors</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tools.Captcha'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ContactForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ContactForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validates</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ContactForm'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ContactForm'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ContactForm'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'message'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">//send email with CakeEmail</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Common</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">flashMessage</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'formContainsErrors'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">helpers</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_merge"><span style="color: #990000;">array_merge</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">helpers</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tools.Captcha'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>As you can see it only sends emails after successfully validating.<br />
For public contact forms I usually like some easy captcha behavior attached so that spam doesnt reach me. You can omit that, of course.<br />
Also note: you should use your own setFlash() method instead of mine!</p>
<h3>Last but not least: View</h3>
<p><code>/View/Contact/index.ctp</code></p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ContactForm'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;fieldset&gt;
		&lt;legend&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contactLegend'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/legend&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'subject'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rows'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Auth.User.id'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Captcha</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ContactForm'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/fieldset&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">submit</span><span style="color: #009900;">&#40;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre>
<h3>Result</h3>
<p>Browse to <code>/contact/</code><br />
That should display the form right away.<br />
After the successful POST you should redirect back to the contact form (emptied then) or to another custom page.</p>
<h3>Final notes</h3>
<p>Currently, the Model is in the Tools.Plugin. You could put it into your normal app model folder, as well.<br />
But I use it in many projects and therefore I want to keep it dry. Feel free to adjust any of the code to your own needs. Same goes for my own custom methods like $this->Common->flashMessage() etc.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=76&amp;md5=86b944a8a8aa56ab32ed4524ba0e6432" title="Flattr" target="_blank"><img src="http://www.dereuromark.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2011/12/15/tools-plugin-part-2-contact-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.dereuromark.de/?flattrss_redirect&amp;id=76&amp;md5=86b944a8a8aa56ab32ed4524ba0e6432" type="text/html" />
	</item>
		<item>
		<title>Unit-Testing Tips for 2.0 and PHPUnit</title>
		<link>http://www.dereuromark.de/2011/12/04/unit-testing-tips-for-2-0-and-phpunit/</link>
		<comments>http://www.dereuromark.de/2011/12/04/unit-testing-tips-for-2-0-and-phpunit/#comments</comments>
		<pubDate>Sat, 03 Dec 2011 23:09:45 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[Unit-Tests]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1387</guid>
		<description><![CDATA[Quite some time ago I wrote about Unit testing. But that was still in 1.3 and with SimpleTest. A lot has changed since then. Execution Order The documentation wasn&#8217;t all that clear about it. So I tried it with the following test file: App::uses&#40;'MyCakeTestCase', 'Tools.Lib'&#41;; &#160; class TestCaseExecutionOrderTest extends MyCakeTestCase &#123; &#160; public function setUp&#40;&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>Quite some time ago I wrote about <a href="http://www.dereuromark.de/2011/04/03/unit-testing-in-cakephp/" title="unit-testing-in-cakephp">Unit testing</a>. But that was still in 1.3 and with SimpleTest.<br />
A lot has changed since then.</p>
<h3>Execution Order</h3>
<p>The documentation wasn&#8217;t all that clear about it. So I tried it with the following test file:</p>
<pre class="php" style="font-family:monospace;">App<span style="color: #339933;">::</span><span style="color: #004000;">uses</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MyCakeTestCase'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tools.Lib'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> TestCaseExecutionOrderTest <span style="color: #000000; font-weight: bold;">extends</span> MyCakeTestCase <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		parent<span style="color: #339933;">::</span><span style="color: #004000;">setUp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'setUp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tearDown<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tearDown'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		parent<span style="color: #339933;">::</span><span style="color: #004000;">tearDown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'startTest'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> endTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'endTest'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'* foo *'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testBar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">out</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'* bar *'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre>
<p>Note the parent calls for setUp and tearDown. Those need to be set. The other 2 methods don&#8217;t require this.</p>
<p>The result was pretty obvious:</p>
<pre class="text" style="font-family:monospace;">setUp
startTest
* foo *
endTest
tearDown
&nbsp;
setUp
startTest
* bar *
endTest
tearDown</pre>
<h3>Debug Output with PHPUnit >= 3.6</h3>
<p>On the release notes of CakePHP2.0.3 you could find the note</p>
<pre class="text" style="font-family:monospace;">A big difference people will notice when writing unit tests is that all output is swallowed by PHPUnit and not presented in either the web tester page nor in the CLI tester. To overcome this annoyance use the--debug modifier if you are using the CLI interface</pre>
<p>Well, that broke a lot of existing debug code, of course.<br />
So I tried to come up with a solution. The Shell uses a specific output(). Why not use sth like that in test cases, too?</p>
<pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * outputs debug information during a web tester (browser) test case
 * since PHPUnit&gt;=3.6 swallowes all output by default
 * this is a convenience output handler since debug() or pr() have no effect
 * @param mixed $data
 * @param bool $pre should a pre tag be enclosed around the output
 * @return void
 * 2011-12-04 ms
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> out<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pre</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pre</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> pre<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;"># cli mode / shell access: use the --debug modifier if you are using the CLI interface
</span>		<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<a href="http://www.php.net/ob_flush"><span style="color: #990000;">ob_flush</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>As you probably noticed, I use it in the above &#8220;execution tryout&#8221;.<br />
You can make yourself a custom &#8220;MyCakeTestCase&#8221; class with then extends the core test case class and put it in there.</p>
<h3>UPDATE January 2012</h3>
<p>The core (>=2.0) now contains a native way to enable debugging output for your tests.<br />
You need to append <code>&#038;debug=1</code> to the url. Not ideal, but it works.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1387&amp;md5=90fe8064b70c28ac327e7542232b9a2e" title="Flattr" target="_blank"><img src="http://www.dereuromark.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2011/12/04/unit-testing-tips-for-2-0-and-phpunit/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1387&amp;md5=90fe8064b70c28ac327e7542232b9a2e" type="text/html" />
	</item>
		<item>
		<title>Serving views as files in Cake2</title>
		<link>http://www.dereuromark.de/2011/11/21/serving-views-as-files-in-cake2/</link>
		<comments>http://www.dereuromark.de/2011/11/21/serving-views-as-files-in-cake2/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 00:09:23 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[cake2.0]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1375</guid>
		<description><![CDATA[Actually, its not that different in Cake1.3. But as I just played around with it in 2.0, I will stick to that version for examples. How to start Skip this, if you want to cut to the chase. In your routes.php you need to add Router::parseExtensions(); (or only specific ones). That tells cake that urls [...]]]></description>
			<content:encoded><![CDATA[<p>Actually, its not that different in Cake1.3. But as I just played around with it in 2.0, I will stick to that version for examples.</p>
<h3>How to start</h3>
<p>Skip this, if you want to cut to the chase.</p>
<p>In your routes.php you need to add <code>Router::parseExtensions();</code> (or only specific ones).<br />
That tells cake that urls ending with &#8220;.xyz&#8221; will be served as files (either inline or as download attachment).</p>
<h3>Setup</h3>
<p>Let&#8217;s say you want to display an invoice as pdf. The normal url is <code>/invoices/view/1</code>.<br />
Now, we set a link in the view to the file like so:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'View as PDF'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'pdf'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>Since cake automatically detects that this will be a pdf file, it will<br />
a) set the correct header (application/pdf)<br />
b) will try to find the specific pdf view in <code>/View/Invoices/pdf/index.ctp</code> and the pdf layout in <code>/View/Layouts/pdf/default.ctp</code></p>
<p>That&#8217; all <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Download it right away</h3>
<p>Files like pdf can be displayed inline. So the browser will usually not force you to download it.<br />
If you want this, though, you need to call <code>$this->request->download($filename);</code> in your controller action or in your layout.</p>
<p>Note: If your browser does not understand the file format (in this case pdf) it will probably trigger the download right away.</p>
<h3>Browser bugs and easter eggs</h3>
<p>Well, you could also call it a bug. But during my tryouts I found out that files served inline (Content-Disposition: inline; filename=&#8221;&#8230;&#8221;) will not use the given filename on save. They will be saved with the name in the url instead. In the above example it would be &#8220;1.pdf&#8221;. I did some research: Thats a well known browser deficiency that nobody yet fixed. Or so it seems.<br />
Ok, but nobody wants his invoice to be &#8220;1.pdf&#8221;. So what can we do about it?</p>
<p>I found a pretty well working workaround:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'View as PDF'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ext'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'pdf'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'invoice-2011-11-01_some_customer_tag'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>As you can see we simply add our filename to the url &#8211; after the id (!).</p>
<p>Since it is only &#8220;filename cosmetics&#8221; we dont need to add this second passed param to our method:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> view<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre>
<p>It will be ignored in the action itself.</p>
<p>So the generated url is <code>/invoices/view/1/invoice-2011-11-01_some_customer_tag.pdf</code> and will result in a file saved as &#8220;invoice-2011-11-01_some_customer_tag.pdf&#8221;.<br />
Job done.</p>
<p>Let me know what you think.</p>
<h3>Example for PDFs</h3>
<p>A pretty quick example how to output your content as pdf using DOMPDF.</p>
<p>In your layout (default.ctp in /pdf/):</p>
<pre class="php" style="font-family:monospace;">App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Vendor'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'dompdf/dompdf.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMPDF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load_html</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/utf8_decode"><span style="color: #990000;">utf8_decode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content_for_layout</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> Configure<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'App.encoding'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">output</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>For me it seemed that without utf8_decode the DOMPDF lib seems to be buggy &#8211; although it claims to support utf8.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1375&amp;md5=a69a663b2d304330180ffe274b273fc6" title="Flattr" target="_blank"><img src="http://www.dereuromark.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2011/11/21/serving-views-as-files-in-cake2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1375&amp;md5=a69a663b2d304330180ffe274b273fc6" type="text/html" />
	</item>
		<item>
		<title>Freshly baked Cake2.0 tips</title>
		<link>http://www.dereuromark.de/2011/10/31/freshly-baked-cake2-0-tips/</link>
		<comments>http://www.dereuromark.de/2011/10/31/freshly-baked-cake2-0-tips/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 20:10:47 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[cake2.0]]></category>
		<category><![CDATA[Elements]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Upgrade]]></category>
		<category><![CDATA[View]]></category>
		<category><![CDATA[Xml]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1295</guid>
		<description><![CDATA[With 2.0 there are many new features available. Some of them I want to introduce here. Using the console for 2.0 I once wrote about how to use the Cake1.3 console. The 2.0 shell is not so much different. The Console folder was only moved to /lib/Cake/Command/. Make sure you use this new path. Also [...]]]></description>
			<content:encoded><![CDATA[<p>With 2.0 there are many new features available.<br />
Some of them I want to introduce here.</p>
<h3>Using the console for 2.0</h3>
<p>I once wrote about <a href="http://www.dereuromark.de/2010/10/03/console-for-cakephp/">how to use the Cake1.3 console</a>.<br />
The 2.0 shell is not so much different. The Console folder was only moved to <code>/lib/Cake/Command/</code>.<br />
Make sure you use this new path. Also note, that it now seems to be important to have a &#8220;base app&#8221;.<br />
You can&#8217;t just use the console anymore anywhere you like. It needs to have an app to get configuration and additional information on.<br />
It also might be important which direction you are using the console &#8211; at least in 2.0 now. Being in the Command path with -app param might result in sth different than using the default way of using the console from within your current app directory.<br />
So you should always go with the latter:</p>
<pre class="php" style="font-family:monospace;">E<span style="color: #339933;">:</span>\<span style="color: #339933;">...</span>\trunk\app\<span style="color: #339933;">&gt;..</span>\lib\Cake\Console\cake <span style="color: #009900;">&#91;</span>command<span style="color: #009900;">&#93;</span></pre>
<p>And if you put your &#8220;&#8230;/Console&#8221; path in your System Environment path (see the other article for details), you can use &#8220;cake&#8221; as standalone again:</p>
<pre class="php" style="font-family:monospace;">E<span style="color: #339933;">:</span>\<span style="color: #339933;">...</span>\trunk\app\<span style="color: #339933;">&gt;</span>cake <span style="color: #009900;">&#91;</span>command<span style="color: #009900;">&#93;</span></pre>
<p>Cronjobs now work this way (using crontab):</p>
<pre class="php" style="font-family:monospace;"><span style="color: #339933;">*/</span><span style="color: #cc66cc;">30</span> <span style="color: #339933;">*</span> <span style="color: #339933;">*</span> <span style="color: #339933;">*</span> <span style="color: #339933;">*</span> <span style="color: #339933;">/</span>srv<span style="color: #339933;">/</span>www<span style="color: #339933;">/.../</span>trunk<span style="color: #339933;">/</span>vendors<span style="color: #339933;">/</span>cakeshell app <span style="color: #339933;">-</span>cli <span style="color: #339933;">/</span>usr<span style="color: #339933;">/</span>bin <span style="color: #339933;">-</span>console <span style="color: #339933;">/</span>srv<span style="color: #339933;">/</span>www<span style="color: #339933;">/.../</span>trunk<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>Cake<span style="color: #339933;">/</span>Console <span style="color: #339933;">-</span>app <span style="color: #339933;">/</span>srv<span style="color: #339933;">/</span>www<span style="color: #339933;">/.../</span>trunk<span style="color: #339933;">/</span>site <span style="color: #339933;">&gt;&gt;</span> <span style="color: #339933;">/</span>tmp<span style="color: #339933;">/</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a><span style="color: #339933;">.</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a></pre>
<p>This runs a the AppShell (defined in your /App/Console/Command/ folder) every 30 minutes and logs errors/results to the given log file.</p>
<p>Note: cakeshell is a file containing the script from the <a href="http://book.cakephp.org/view/1110/Running-Shells-as-cronjobs">cookbook</a>.<br />
Also note: If you use windows you might need to run &#8220;dos2unix /&#8230;/Cake/Console/cake&#8221; in order to make the cake console working.</p>
<h3>Upgrade using the upgrade shell</h3>
<pre class="php" style="font-family:monospace;">cake upgrade all <span style="color: #339933;">-</span>params</pre>
<p>I use an <a href="http://cakephp.lighthouseapp.com/projects/42648/tickets/2117-improvements-for-20-upgrade-shell">enhanced shell which provides more functionality</a>.<br />
For params I use &#8211;svn. It cakes care of the moving process if you use subversion. git is also supported.<br />
Even if you already upgraded you should run it. Just in case you missed anything. The shell also auto-corrects some styling errors or deprecated syntax.</p>
<h3>Rendering elements from helpers</h3>
<p>This is now possible because the View is available in the helpers now.<br />
So inside a helper method just do:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$elementContent</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_View<span style="color: #339933;">-&gt;</span><span style="color: #004000;">element</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>Note: element() now has three params. The second still is the one where you can pass variables to the element scope. But the third param is now the options param (cache, plugin, callbacks, &#8230;).</p>
<p>It is even possible to alter the output from a helper directly:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_View<span style="color: #339933;">-&gt;</span><span style="color: #004000;">output</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// contains the current rendered output</span></pre>
<p>If you need any information on the current url or params:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_View<span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span> <span style="color: #666666; font-style: italic;">// contains all information about the current request</span></pre>
<h3>Overriding plugin views in the app</h3>
<p>Another feature that finally arrived. Now plugins don&#8217;t have to cover all possible cases.<br />
For specific apps you can override the plugin view locally.<br />
Place the view in <code>/app/View/Plugin/PluginName/Controller/action.ctp</code>. The same goes for layouts, of course.</p>
<h3>Enhancing objects and aliasing</h3>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$helpers</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'Html'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'className'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Tools.HtmlExt'</span> <span style="color: #666666; font-style: italic;">// Tools Plugin - HtmlExtHelper</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>Declared anywhere in the code the HtmlExt helper will then be used instead of the core Html helper.<br />
The important change is, that it will use the old namespace, though. $this->Html is still the right way to use it. So you can enhance every helper, component, &#8230; to suite your needs without having to change the existing code itself.</p>
<p>This opens whole new possibilities. Until now you would have used a FormExt helper and manually called it with $this->FormExt->foo().<br />
For 2.0 I use and recommend the following structure:<br />
Add Ext to helpers/components etc from the core you want to Extend:<br />
- FormExt(Helper)<br />
- RequestHandlerExt(Component)<br />
alias them as above to the original one.<br />
Best to place them in a plugin all your apps have in common (Tools in my case).<br />
Now if you want to go even further, you can override that one again specifically for this app by prefixing it with My:<br />
- MyForm<br />
- MyFormExt<br />
&#8230;<br />
All app specific helpers are not in a plugin but the app dir structure and start with My.<br />
And using aliasing you can still call the helper with $this->Form->foo().<br />
Same goes for components, behaviors, &#8230;</p>
<h3>Working with exceptions</h3>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> NotFoundException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>The exception should then display a 404 error page.</p>
<h3>Including scripts</h3>
<p>Before 2.0 you always used App::import();<br />
But with the switch to App::uses() the files do not get included anymore at startup due to the Super-lazy-loading. This should only be used for real classes. Also start to group by PackageName:</p>
<pre class="php" style="font-family:monospace;">App<span style="color: #339933;">::</span><span style="color: #004000;">uses</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GeolocationLib'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tools.Geo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;"># in /Plugin/Tools/Geo</span></pre>
<p>It will only be included by Cake if actually needed. One reason why Cake2 is so much faster.</p>
<p>If you have a file without a class (containing functions or constants or whatever) you still need to use the old method:</p>
<pre class="php" style="font-family:monospace;">App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Lib'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tools.Bootstrap/MyBootstrap'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># in package &quot;Bootstrap&quot;</span></pre>
<h3>Using own classes instead of core ones</h3>
<p>Let&#8217;s say, you want to apply a fix to a core file without overriding the core folder. Or you want to replace a file altogether. Simply use the same folder structure inside the /Lib folder.<br />
For your own &#8220;FormAuthenticate&#8221;<br />
<code>/app/Lib/Controller/Component/Auth/FormAuthenticate.php</code></p>
<h3>Cleanup!</h3>
<p>If you are upgrading to 2.0 &#8211; or already have &#8211; you should get rid of old PHP4 chunk.<br />
For starters, the &#038;+object needs to get eradicated once and for all.</p>
<p>Old style:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setup<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$Model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre>
<p>A correct piece of a behavior now looks like:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setup<span style="color: #009900;">&#40;</span>Model <span style="color: #000088;">$Model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;"># type Model and no &amp;</span></pre>
<p>Same goes for components, controllers, helpers, &#8230;<br />
Note: You can do this for 1.3 already, as well. At least if you are using PHP5 (and you probably are).</p>
<p>You should also change all &#8220;var $x&#8221; to &#8220;public $x&#8221; and add &#8220;public&#8221; to all methods in classes which don&#8217;t have a visibility attribute yet.</p>
<p>I wrote a &#8220;CorrectShell&#8221; which extends the UpgradeShell. It does take care of almost all changes. I will publish it soon.</p>
<h3>Things I did find out the hard way</h3>
<p>Most of my libs and helpers which use the Xml class had to be modified quite a bit.<br />
The class itself is not accessed statically, and more important does return only lowercase keys.<br />
This was some piece of work to find and correct all those necessary changes.</p>
<p>If you make use of the internal HttpSocket class you might want to be interested in the fact, that it now returns an object as response. So you need to use $response->body to access the result content.</p>
<p>You cannot really set the headers and content type in your layouts anymore manually using &#8220;header()&#8221;. You need to do this using the response object:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//or as mimetype:</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'application/pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>Either from the controller or the view/layout/helper.</p>
<p>The core.php, routes.php and bootstrap.php need new settings in order for your app to fully function under 2.0. Tip: Look at the original 2.0 app files and compare them to your upgraded files.<br />
Add anything that is missing.</p>
<p>There seem to be dependency problems now with components and helpers. So the order in which you add them in your controller is now important (wasn&#8217;t in 1.3). If they depend on a class you should add them after that one to prevent the collection to fatal error. (UPDATE: Some if it seems to be fixed now).</p>
<h3>Shell tips not mentioned in any upgrade guide</h3>
<p>Shells are one of the most useful tools out there. I have dozens of them and those were the first classes I ported to 2.0. Therefore I ran into problems with those, first. So in case, you write your own ones, too:</p>
<p>The usage of shells has changed quite a lot. You don&#8217;t need a help method anymore. The consoleOptionParser object will take care of that using &#8220;-h&#8221;.<br />
For using params/args you will now need to set up a &#8220;getOptionParser&#8221; method (see the core shells for examples).<br />
For string options you should set an empty default value: <code>'default' => ''</code> (prevents you from warnings). For boolean values set <code>'boolean' => true</code>.</p>
<p>A main() command should always be called explicitly (&#8220;cake Shell main -f&#8221; instead of &#8220;cake Shell -f&#8221;) if you want to pass params or args. Otherwise it won&#8217;t run.</p>
<h3>And there is more</h3>
<p>Make sure you check all your forms. Especially the ones where you customized. Since 2.0 now adds HTML5 types automatically, it can get unwanted results when you use names for fields that are already matched in the schema. In my case many normal &#8220;text&#8221; fields suddenly ended up being &#8220;type&#8221;=>&#8221;number&#8221;. Which screwed up the whole form (could not validate anymore).<br />
Some normal selects (for search etc) suddenly ended up being &#8220;multiple&#8221;=>&#8221;multiple&#8221;. Why? I am not yet sure <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The FormHelper does not support some field type aliases anymore. So for textareas you have to use <code>'type'=>'textarea'</code>. &#8220;textfield&#8221; doesn&#8217;t work anymore &#8211; if you have that still somewhere in your forms.</p>
<p>Custom Joins do not need the prefix anymore. In 1.3 you had to do:</p>
<pre class="php" style="font-family:monospace;"><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'table'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Conversation</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tablePrefix</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'conversation_messages'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'alias'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'ConversationMessage'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'type'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'inner'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Conversation.last_message_id = ConversationMessage.id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #339933;">...</span></pre>
<p>In 2.0 it&#8217;s just:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'table'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'conversation_messages'</span></pre>
<p>Otherwise it will prepend the prefix twice which, of course, breaks the query!</p>
<p>The Routing has changed quite a bit. Some to the better &#8211; some to the worse (or I coudn&#8217;t figure out yet how it is done in 2.0):<br />
In 1.3 this was working to connect an url and all its prefixes:</p>
<pre class="php" style="font-family:monospace;">Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/shortcut'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'overview'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'some_action'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>But this will not in 2.0 anymore. Here all prefixes get treated as normal named params.<br />
A quick fix (which does not enable the prefixed urls, though!) would be:</p>
<pre class="php" style="font-family:monospace;">Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/shortcut'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin'</span><span style="color: #339933;">=&gt;</span>false<span style="color: #339933;">,</span> <span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'overview'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'some_action'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
<p>At least the normal user url is working, again.<br />
Although this doesn&#8217;t seem to work flawlessy, either&#8230;</p>
<p>Custom joins don&#8217;t need a model prefix anymore:</p>
<pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'joins'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span>
	<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'table'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tablePrefix</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'conversation_users'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'alias'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'ConversationUser'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'type'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'inner'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Conversation.id = ConversationUser.conversation_id'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span></pre>
<p>becomes</p>
<pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'joins'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span>
	<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'table'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'conversation_users'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'alias'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'ConversationUser'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'type'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'inner'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Conversation.id = ConversationUser.conversation_id'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span></pre>
<p>Cake is now capable of prefixing the table names automatically.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1295&amp;md5=491740adebf77311ef1daa1989f8e595" title="Flattr" target="_blank"><img src="http://www.dereuromark.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2011/10/31/freshly-baked-cake2-0-tips/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<atom:link rel="payment" href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1295&amp;md5=491740adebf77311ef1daa1989f8e595" type="text/html" />
	</item>
		<item>
		<title>More great news: CakePHP 2.0 stable is out!</title>
		<link>http://www.dereuromark.de/2011/10/18/more-great-news-cake2-0-stable-is-out/</link>
		<comments>http://www.dereuromark.de/2011/10/18/more-great-news-cake2-0-stable-is-out/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 00:01:03 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[cake2.0]]></category>
		<category><![CDATA[Migration]]></category>
		<category><![CDATA[PHPUnit]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1282</guid>
		<description><![CDATA[After 1.5 years of development, Cake catches up with other frameworks. It is now as modern as most of the others while remaining the most powerful of all. Almost none if its automatic got lost during the process of rewriting. Some of the important aspects of 2.0 Faster (20-40% depending on the application) Modern (>=PHP5.2 [...]]]></description>
			<content:encoded><![CDATA[<p>After 1.5 years of development, Cake catches up with other frameworks.<br />
It is now as modern as most of the others while remaining the most powerful of all.<br />
Almost none if its automatic got lost during the process of rewriting.</p>
<h3>Some of the important aspects of 2.0</h3>
<ul>
<li>Faster (20-40% depending on the application)</li>
<li>Modern (>=PHP5.2 and state of the art functionality)</li>
<li>More Flexible and extensible (almost every class can be switched out with own ones)</li>
<li>Exceptions as error handling</li>
<li>PHPUnit as test suite</li>
<li>Hundreds of fixes which had to stay in 1.3 due to compatibility issues</li>
<li>Lots of enhancements and new functionality like &#8220;aliasing&#8221; &#8211; <a href="http://book.cakephp.org/2.0/en/appendices/new-features-in-cakephp-2-0.html">details</a></li>
</ul>
<p>If you start a new project dive right into 2.0. That&#8217;s the future <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>For all those who like to assimilate the changes as online video:<br />
<a href="http://tv.cakephp.org/video/CakeFoundation/2011/10/06/ch-ch-anges_cakephp_2_0_-_by_mark_story">tv.cakephp.org/video/CakeFoundation/2011/10/06/ch-ch-anges_cakephp_2_0_-_by_mark_story</a></p>
<h3>Tips for upgrading </h3>
<p>The <a href="http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html">migration guide</a> will help to upgrade existing applications to the new version.<br />
This will be quite a bit of work, though. Almost all classes are renamed, many methods and object variables have been dropped.</p>
<p>Your biggest help will be the cake shell script &#8220;ugrade&#8221; which is available in 2.0 now. It helps you to automatically upgrade some of the code. You can either select all or single tasks like renaming components etc.<br />
Simply type &#8220;cake upgrade&#8221; in the console to list all available methods.<br />
It really is a huge time safer. You should backup or commit everything before you attempt to run this script, though.</p>
<p><strong>UPDATE:</strong><br />
I tried to upgrade a medium sized 1.3 app to 2.0. After finding several problems I opened a <a href="http://cakephp.lighthouseapp.com/projects/42648/tickets/2117-improvements-for-20-upgrade-shell">ticket</a>.<br />
Hopefully the upgrade process will be made smoother the next couple of weeks <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Last Words</h3>
<p>I am a little bit proud to see many of my tickets and proposed changes/fixes being now part of the framework.<br />
Contributing to such a popular and large framework is sometimes easier than one might think. Sometimes it needed only hours to approve one of my tickets. OK, sometimes it needs months.<br />
Providing a good reason and attaching proposed changes as diff or patch can really speed up things and should encourage you to involve yourself in cake core development, as well.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1282&amp;md5=05116bd0a59eaf3845210eb918fe4e45" title="Flattr" target="_blank"><img src="http://www.dereuromark.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2011/10/18/more-great-news-cake2-0-stable-is-out/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1282&amp;md5=05116bd0a59eaf3845210eb918fe4e45" type="text/html" />
	</item>
		<item>
		<title>Finally &#8211; SVN becomes more GITish</title>
		<link>http://www.dereuromark.de/2011/10/17/finally-svn-becomes-more-gitish/</link>
		<comments>http://www.dereuromark.de/2011/10/17/finally-svn-becomes-more-gitish/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 00:12:23 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[SVN]]></category>
		<category><![CDATA[toirtoise svn]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1274</guid>
		<description><![CDATA[The change we all had waited for soo long. SVN becomes more GITish With the release of all new 1.7 the thousands of .svn folders are finally history! Only a single .svn file in the root folder remains. Yes, most of you will immediately think of GIT Well, now SVN uses a sqllite DB to [...]]]></description>
			<content:encoded><![CDATA[<p>The change we all had waited for soo long.</p>
<h3>SVN becomes more GITish</h3>
<p>With the release of all new 1.7 the thousands of .svn folders are finally history!<br />
Only a single .svn file in the root folder remains. Yes, most of you will immediately think of GIT <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Well, now SVN uses a sqllite DB to work the same way.<br />
The speed increased, as well.</p>
<p>All you need to do on windows:<br />
- Update TortoiseSVN<br />
- Rightclick on the root folder of your svn folder and select &#8220;SVN update working copy&#8221;</p>
<p>Details:<br />
<a href="http://tortoisesvn.net/tsvn_1.7_releasenotes.html">tortoisesvn.net/tsvn_1.7_releasenotes.html</a></p>
<h3>Numbers wanted?</h3>
<p>To demonstrate how huge this step in the right direction is:<br />
&#8220;cake2.0rc3&#8243; &#8211; the current 2.0 head &#8211; consists of 1518 files.</p>
<p>Before the upgrade to tortoisesvn1.7 the complete folder consisted of 4100 files in 5100 folders. So almost twice as much svn crap &#8211; overall 38 MB in size.<br />
After the upgrade the folder counts 2826 files in 848 folders (wow!). File size is now down to 32 MB.</p>
<p>I really like it. Copying/Moving is now so much easier. I had just recently written a script which could delete all tmp folders (.svn) from a directory recursively to be able to merge it into other folders.<br />
This is now history. Merging finally works as easy as in GIT.<br />
Backuping and remote copying is now so much faster due to less structural overhead.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1274&amp;md5=eada0fe796b2eec8334cad3f237cda33" title="Flattr" target="_blank"><img src="http://www.dereuromark.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.dereuromark.de/2011/10/17/finally-svn-becomes-more-gitish/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1274&amp;md5=eada0fe796b2eec8334cad3f237cda33" type="text/html" />
	</item>
	</channel>
</rss>

