<?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>Tue, 08 May 2012 13:21:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Schema reference for table less models</title>
		<link>http://www.dereuromark.de/2012/05/01/schema-reference-for-table-less-models/</link>
		<comments>http://www.dereuromark.de/2012/05/01/schema-reference-for-table-less-models/#comments</comments>
		<pubDate>Tue, 01 May 2012 10:16:52 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Cake2]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Model]]></category>
		<category><![CDATA[Schema]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1821</guid>
		<description><![CDATA[If you ever need a table less model with only some manual schema information (mainly for validation and form helper to work), you might find the documentation a little bit incomplete. There are more than just those types available. Here is a full list. PS: Quite some time ago, I used this for my ContactForm. [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever need a table less model with only some manual schema information (mainly for validation and form helper to work), you might find the <a href="http://book.cakephp.org/2.0/en/models/model-attributes.html#schema">documentation</a> a little bit incomplete.
There are more than just those types available.
Here is a full list.</p>

<p>PS: Quite some time ago, I used this for my <a href="http://www.dereuromark.de/2011/12/15/tools-plugin-part-2-contact-form/">ContactForm</a>. This is a live example on how to use such a table less model and how validation can still be performed on these virtual fields.</p>

<h3>How is it used?</h3>

<p><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MySchemaLessModelName <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$&#95;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;">'status'</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;">'boolean'</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
           <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</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;">'comment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'some optional comment'</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>
<span style="color: #009900;">&#125;</span></pre>
Why is this necessary/recommended?</p>

<p>By manually describing your table less fields cake you can have the same form helper magic. So the FormHelper will display a checkbox for boolean fields, and textareas for text fields etc.
Also you get all the validation (length or required for example) out of the box. You can also set up the $validate array for additional validation, of course.</p>

<h3>Full schema reference</h3>

<p>Boolean (tinyint 1):
<pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'approved'</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;">'boolean'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</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;">'comment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'some optional comment'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></pre>
Best to make this field unsigned in your database (although cake cannot recognize this as of now).</p>

<p>Integer (int 10/11):
<pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'clicks'</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;">'integer'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</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;">'comment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'some optional comment'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></pre>
This field can also be unsigned if there cannot be any negative values.</p>

<p>String (varchar 1&#8230;255):
<pre class="php" style="font-family:monospace;"><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;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">255</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;">'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;">'comment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'some optional comment'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></pre></p>

<p>Text (text):
<pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'comment'</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;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</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;">'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;">'comment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'some optional comment'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></pre></p>

<p>Decimal (float):
<pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'amount'</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;">'float'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'6,2'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">0.00</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;">'comment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'some optional comment'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></pre></p>

<p>Foreign key as UUID (char 36):
<pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'user&#95;id'</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;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">36</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;">'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;">'comment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'some optional comment'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></pre></p>

<p>Date (date)
<pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'published'</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;">'date'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</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;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'comment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'some optional comment'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></pre></p>

<p>Datetime (datetime)
<pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'modified'</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;">'datetime'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</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;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'comment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'some optional comment'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></pre></p>

<p>Enum (at least my version of it &#8211; since cake doesn&#8217;t have a core solution here)
<pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">'status'</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;">'integer'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</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;">'comment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'some optional comment'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></pre>
See <a href="http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/">static-enums-or-semihardcoded-attributes</a> for how to use these tinyint(2) enums.</p>

<p>Note: All fields (except for date/datetime) are set to &#8220;not null&#8221; in the above examples (which will make the form helper and validation require those). Those can be null, though, too, if desired.</p>

<h3>Hot Tip</h3>

<p>If you are looking for a quick way to find this out yourself:</p>

<p>Create a table &#8220;apples&#8221; and an Apple model and add all types of fields you want to debug then call the model schema() like so:
<pre class="php" style="font-family:monospace;">debug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Apple</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">schema</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
This is how I confirmed the above.</p>

<p>A second more persistent way would be to run the following command for such a test table (or any other real table for that matter):
<pre class="php" style="font-family:monospace;">cake schema generate</pre>
It will dump a schema file in your <code>APP/Config/Schema</code> folder with the current tables as cake schema definition.</p>

<h3>Anything missing?</h3>

<p>I hope I didn&#8217;t forget anything. Otherwise please correct me!</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1821&amp;md5=776b671dbc17e18f6e5b4663091bf473" 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/05/01/schema-reference-for-table-less-models/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=dereuromark&amp;popout=1&amp;url=http%3A%2F%2Fwww.dereuromark.de%2F2012%2F05%2F01%2Fschema-reference-for-table-less-models%2F&amp;language=de_DE&amp;category=text&amp;title=Schema+reference+for+table+less+models&amp;description=If+you+ever+need+a+table+less+model+with+only+some+manual+schema+information+%28mainly+for+validation+and+form+helper+to+work%29%2C+you+might+find+the+documentation+a+little+bit...&amp;tags=Cake2%2CDatabase%2CModel%2CSchema%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Cake Bake: Custom Templates &#8220;Deluxe&#8221;</title>
		<link>http://www.dereuromark.de/2012/04/24/cake-bake-custom-templates-deluxe/</link>
		<comments>http://www.dereuromark.de/2012/04/24/cake-bake-custom-templates-deluxe/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 19:00:20 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Bake]]></category>
		<category><![CDATA[cake2.x]]></category>
		<category><![CDATA[Custom Template]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1913</guid>
		<description><![CDATA[You might have read my almost two year old article about custom bake templates. Much changed since then. In Cake as well in my user-land code. Note: The &#8220;setup&#8221; bake theme is written for 2.x but can easily be backported to 1.3 manually. Outline We probably all know how xxxxxxx powerful the Bake tool is. [...]]]></description>
			<content:encoded><![CDATA[<p>You might have read my almost two year old article about <a href="http://www.dereuromark.de/2010/06/22/cake-bake-custom-templates/">custom bake templates</a>.
Much changed since then. In Cake as well in my user-land code.</p>

<p>Note: The &#8220;setup&#8221; bake theme is written for 2.x but can easily be backported to 1.3 manually.</p>

<h3>Outline</h3>

<p>We probably all know how xxxxxxx powerful the Bake tool is. It is all about speed for the first outline of a project.
So the faster the first CRUD controllers are baked the sooner we can dig into the details.
But speed itself is not the only crucial part. If we have a model/table with a lot of meta fields that should not get baked we would have to remove them manually from index/view/form every time we re-bake. That&#8217;s not really something you want to waste your time with.</p>

<h3>Skip Fields</h3>

<p>With some custom template like mine you can skip certain fields for baking. Some like <code>'password', 'slug', 'lft', 'rght'</code> are automatically skipped by my template. But you can also define additional fields yourself:
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Deadline <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$scaffoldSkipFields</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;">'note'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
So the textarea field &#8220;note&#8221; will not be outputted in your ctp files.</p>

<p>An additional feature I want to implement is that you can specify the actions it should skip:
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$scaffoldSkipFields</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;">'note'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'prefix'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'actions'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
This would mean that it skips this field for all non-prefixed ctps as well as all indexes.
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$scaffoldSkipFields</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;">'note'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'prefix'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'admin'</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;">// skip for all admin views</span></pre>
This will be included shortly.</p>

<h3>Default Values</h3>

<p><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$scaffoldDefaultValues</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;">'role&#95;id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ROLE&#95;USER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User::STATUS&#95;ACTIVE'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'level'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'\'foo\''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
Which would result in this inside the controller action <code>add</code>:
<pre class="php" style="font-family:monospace;"><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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">...</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;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'role&#95;id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> ROLE&#95;USER<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;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'status'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> User<span style="color: #339933;">::</span><span style="color: #004000;">STATUS&#95;ACTIVE</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;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'level'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</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;">'User'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'foo'</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre>
This will have the above default values set up for adding a new user.</p>

<p>PS: Why not in the form itself? It is always better to put default values as a part of the logic into the controller and outside of the view layer.</p>

<h3>Enum support</h3>

<p><a href="http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/">This article</a> describes how to setup enums in CakePHP the easy way. Do this after you bake the model and BEFORE you bake controller and views. Then you will end up with ctps including all the Enum things automatically like a dropdown box for forms and translated values for index and view.</p>

<h3>Smarter theme</h3>

<p>We already covered a few basic field types before:
<pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$schema</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'date'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// formatted date</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$schema</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'boolean'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// yesNo image</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$schema</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// nl2br(h())</span>
<span style="color: #009900;">&#125;</span></pre>
I also added decimals for currencies:
<pre class="php" style="font-family:monospace;">} elseif ($schema[$field]['type'] == 'float' &amp;&amp; strpos($schema[$field]['length'], ',') !== false) {
    echo &quot;\t\t&lt;dd&gt;\n\t\t\t<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;">Numeric</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">money</span><span style="color: #009900;">&#40;</span>\$<span style="color: #009900;">&#123;</span><span style="color: #000088;">$singularVar</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'{$modelClass}'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'{$field}'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>\n\t\t&lt;/dd&gt;\n&quot;;
}</pre>
Switch Numeric with Number if you use your own helper. This will print a nicely formatted currency value instead of the default decimal value.</p>

<h3>Usage</h3>

<p>The files are in the <a href="https://github.com/dereuromark/setup/tree/master/Console/Templates">Templates folder of my setup plugin</a>.</p>

<p>Since my plugin theme might contain stuff you cannot and don&#8217;t wont to use you can cherrypick the above &#8220;tricks&#8221; for your own custom bake theme.</p>

<p>Now you know how you can further customize your themes to make baking even more fun as it already is <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1913&amp;md5=e109391a0b5c8c0f363afb9f523d6b68" 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/04/24/cake-bake-custom-templates-deluxe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=dereuromark&amp;popout=1&amp;url=http%3A%2F%2Fwww.dereuromark.de%2F2012%2F04%2F24%2Fcake-bake-custom-templates-deluxe%2F&amp;language=de_DE&amp;category=text&amp;title=Cake+Bake%3A+Custom+Templates+%26%238220%3BDeluxe%26%238221%3B&amp;description=You+might+have+read+my+almost+two+year+old+article+about+custom+bake+templates.+Much+changed+since+then.+In+Cake+as+well+in+my+user-land+code.+Note%3A+The+%26%238220%3Bsetup%26%238221%3B+bake...&amp;tags=Bake%2Ccake2.x%2CCakePHP%2CCustom+Template%2Cblog" type="text/html" />
	</item>
		<item>
		<title>More interesting links</title>
		<link>http://www.dereuromark.de/2012/04/20/more-interesting-links/</link>
		<comments>http://www.dereuromark.de/2012/04/20/more-interesting-links/#comments</comments>
		<pubDate>Fri, 20 Apr 2012 02:32:37 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[WebDevelopment]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[Decorator]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1779</guid>
		<description><![CDATA[This week I have only a few links to offer Periodic Table A pretty geeky site shows the number of elements used on a website as a periodic table: periodic-table. Move over the elements with your mouse to see the count. A CSS3 Generator This Generator can easily generate valid and clean CSS3. CakePHP Decorator [...]]]></description>
			<content:encoded><![CDATA[<p>This week I have only a few links to offer <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<h3>Periodic Table</h3>

<p>A pretty geeky site shows the number of elements used on a website as a periodic table:
<a href="http://joshduck.com/periodic-table.html">periodic-table</a>.
Move over the elements with your mouse to see the count.</p>

<h3>A CSS3 Generator</h3>

<p>This <a href="http://css3generator.com/">Generator</a> can easily generate valid and clean CSS3.</p>

<h3>CakePHP Decorator Approach</h3>

<p>I am not yet sure what to think about the <a href="http://joeytrapp.com/posts/cakephp-decorator-plugin">cakephp-decorator-plugin</a>. Will have to dig into it a little bit deeper. It seems that the decorator here uses markup at a very early stage. Maybe too early? It might be better to address this further down (on a helper level). I will sure look into it some time soon.</p>

<p>.. more coming</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1779&amp;md5=cedc2f08c785c4e97432b753d93946c5" 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/04/20/more-interesting-links/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=dereuromark&amp;popout=1&amp;url=http%3A%2F%2Fwww.dereuromark.de%2F2012%2F04%2F20%2Fmore-interesting-links%2F&amp;language=de_DE&amp;category=text&amp;title=More+interesting+links&amp;description=This+week+I+have+only+a+few+links+to+offer+Periodic+Table+A+pretty+geeky+site+shows+the+number+of+elements+used+on+a+website+as+a+periodic+table%3A+periodic-table....&amp;tags=CakePHP%2CCSS3%2CDecorator%2CXHTML%2Cblog" type="text/html" />
	</item>
		<item>
		<title>CakePHP now fully MVC</title>
		<link>http://www.dereuromark.de/2012/04/10/cakephp-now-fully-mvc/</link>
		<comments>http://www.dereuromark.de/2012/04/10/cakephp-now-fully-mvc/#comments</comments>
		<pubDate>Tue, 10 Apr 2012 10:18:15 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[cake2.x]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1870</guid>
		<description><![CDATA[Yeah I was waiting for this day for quite a long time. And pretty happy that my proposal from last year is finally in the live code. Cake2.1 is now fully, really and completely MVC &#8211; and officially 200% more awesome, of course! MCV? Model - View - Controller. Mainly the basic principle of many [...]]]></description>
			<content:encoded><![CDATA[<p>Yeah <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I was waiting for this day for quite a long time. And pretty happy that my <a href="http://cakephp.lighthouseapp.com/projects/42648/tickets/2140-some-helpers-should-swap-methods-to-libs">proposal from last year</a> is finally in the live code.
Cake2.1 is now fully, really and completely MVC &#8211; and officially 200% more awesome, of course!</p>

<h3>MCV?</h3>

<p><pre class="php" style="font-family:monospace;">Model <span style="color: #339933;">-</span> View <span style="color: #339933;">-</span> Controller<span style="color: #339933;">.</span></pre>
Mainly the basic principle of many modern frameworks that helps to code in a clean way. <a href="http://book.cakephp.org/2.0/en/cakephp-overview/understanding-model-view-controller.html">Details here</a></p>

<h3>What&#8217;s the big deal?</h3>

<p>It had been bugging me for years that we had to do something like this in our controllers and models:
<pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># hold on tight - bad part - start (already cake 2.0 style):
</span>App<span style="color: #339933;">::</span><span style="color: #004000;">uses</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'TimeHelper'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'View/Helper'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
App<span style="color: #339933;">::</span><span style="color: #004000;">uses</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'View'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'View'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Time</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TimeHelper<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> View<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Your flier expired on '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$Time</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$flier</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Flier'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'expires'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># flash message or email title etc
</span><span style="color: #666666; font-style: italic;"># bad part - end</span></pre>
We had to import a View and the Helper only to keep it dry and reuse the methods we already have.</p>

<p>With Cake2.1 we can now do:
<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;">'CakeTime'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Utility'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Your flier expired on '</span> <span style="color: #339933;">.</span> CakeTime<span style="color: #339933;">::</span><span style="color: #004000;">nice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$flier</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Flier'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'expires'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></p>

<p>Same thing with NumberHelper and CakeNumber:
<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;">'CakeNumber'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Utility'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$filesizeString</span> <span style="color: #339933;">=</span> CakeNumber<span style="color: #339933;">::</span><span style="color: #004000;">toReadableSize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filesize</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
instead of the helper overhead similar to above.</p>

<p>Last but not least the TextHelper methods have also been moved to the String class:
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$shortext</span> <span style="color: #339933;">=</span> String<span style="color: #339933;">::</span><span style="color: #004000;">truncate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$longtext</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
Only the autoLink() methods still remain in the helper. Mainly because they contain markup transformation (adding HTML) which belongs to the View layer.</p>

<p>And for the View layer itself nothing changes. The helpers work as before, only they now use the lib classes internally.</p>

<h3>Going one step further</h3>

<p>You can even pass in your own engines (lib classes) for the view helpers.
For example by passing it into the helper settings:
<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: #339933;">...,</span> <span style="color: #0000ff;">'Time'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'engine'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Tools.TimeLib'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></p>

<p>I use this approach:
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> TimeExtHelper <span style="color: #000000; font-weight: bold;">extends</span> TimeHelper <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> &#95;&#95;construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$View</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$settings</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: #000088;">$settings</span> <span style="color: #339933;">=</span> Set<span style="color: #339933;">::</span><span style="color: #004000;">merge</span><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;">'engine'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Tools.TimeLib'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$settings</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// instead of core CakeTime lib</span>
        parent<span style="color: #339933;">::</span>&#95;&#95;construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$View</span><span style="color: #339933;">,</span> <span style="color: #000088;">$settings</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>
It will then use <code>/APP/Plugin/Tools/Lib/Utility/TimeLib.php</code> instead.
Then I alias it back to &#8220;Time&#8221; (same for Number and Text) and everything is like it was before, only that it uses my enhanced lib classes:
<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: #339933;">...,</span> <span style="color: #0000ff;">'Time'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'className'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Tools.TimeExt'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
This way you can even enhance the helper itself (add some more functionality etc).</p>

<h3>But if&#8230;</h3>

<p>&#8230;if you still need to use helpers in the controller or model context?
Well, then you are obviously still doing something wrong.
There is absolutely no need for it anymore. And now no excuses anymore, either <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p>You should move your code to the Lib folder as a package. Then use it in your controller as Lib class and in your View call the helper which then calls the Lib class. This way you keep it dry and simple. Just look how it is done in those classes above in the core.</p>

<h4>Example: <code>MathHelper.php</code></h4>

<p>We might need its basic methods in the controller somewhere. The helper only adds markup or View related goodies.
So we have a MathLib class in <code>APP/Lib/Math</code> (the <code>Math</code> package can also be any other name you want).
And a MathHelper class in <code>APP/View/Helper</code> which uses the MathLib.
Either the lazy way:
<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> &#95;&#95;call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <a href="http://www.php.net/call&#95;user&#95;func&#95;array"><span style="color: #990000;">call&#95;user&#95;func&#95;array</span></a><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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Math</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
Or the verbose way for every single method:
<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> calculate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">MathLib</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calculate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
The latter has the advantage of type-hinting in your IDE.</p>

<p>And in the controller we do (let&#8217;s say we put the code in a &#8220;Tools&#8221; plugin):
<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;">'MathLib'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tools.Math'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># would then be in /APP/Plugin/Tools/Lib/Math/
</span><span style="color: #000088;">$newValue</span> <span style="color: #339933;">=</span> MathLib<span style="color: #339933;">::</span><span style="color: #004000;">calculate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre> 
And in the view we can still use the helper as always:
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tools.Math'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># would be in /APP/Plugin/Tools/View/Helper/
</span><span style="color: #000088;">$newValue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Math</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calculate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></p>

<p>PS: I like to suffix my lib classes with <code>Lib</code> to avoid naming conflicts with Models or other cakephp or php classes. This is not required, of course. But I would recommend it as long we cannot use namespaces (which will be Cake3.0 and is still quite a few miles away).</p>

<p>Happy coding!</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1870&amp;md5=4f0388cb2f3e3ee70e68de2a42ecad48" 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/04/10/cakephp-now-fully-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=dereuromark&amp;popout=1&amp;url=http%3A%2F%2Fwww.dereuromark.de%2F2012%2F04%2F10%2Fcakephp-now-fully-mvc%2F&amp;language=de_DE&amp;category=text&amp;title=CakePHP+now+fully+MVC&amp;description=Yeah+I+was+waiting+for+this+day+for+quite+a+long+time.+And+pretty+happy+that+my+proposal+from+last+year+is+finally+in+the+live+code.+Cake2.1+is+now...&amp;tags=cake2.x%2CCakePHP%2CMVC%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Auth &#8211; inline authorization the easy way</title>
		<link>http://www.dereuromark.de/2012/04/07/auth-inline-authorization-the-easy-way/</link>
		<comments>http://www.dereuromark.de/2012/04/07/auth-inline-authorization-the-easy-way/#comments</comments>
		<pubDate>Sat, 07 Apr 2012 11:41:25 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[Auth]]></category>
		<category><![CDATA[Authentication]]></category>
		<category><![CDATA[Authorization]]></category>
		<category><![CDATA[cake2.x]]></category>
		<category><![CDATA[Roles]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1836</guid>
		<description><![CDATA[I wrote a wrapper class to make inline authorization easier. Often times you want to check on certain roles inside an action or view and depending on the result display specific content or execute specific code. As an example we only want to display the &#8220;admin infos&#8221; box on the home screen for an admin. [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a wrapper class to make inline authorization easier.
Often times you want to check on certain roles inside an action or view and depending on the result display specific content or execute specific code.
As an example we only want to display the &#8220;admin infos&#8221; box on the home screen for an admin. All other users should not see this box.</p>

<h3>Status quo</h3>

<p>We would need to check the session manually against the roles we want to grant access to. This can get pretty hairy with more than one role allowed (if admins and moderators are allowed to see this box for example).</p>

<h3>Preparations</h3>

<p>We first need to make the class usable by putting this in our <code>/Config/bootstrap.php</code>:
<pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// these IDs match the role&#95;ids in the DB</span>
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ROLE&#95;SUPERADMIN'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ROLE&#95;ADMIN'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ROLE&#95;MOD'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'3'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ROLE&#95;USER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'4'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// enable the Auth class</span>
App<span style="color: #339933;">::</span><span style="color: #004000;">uses</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Auth'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tools.Lib'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
I like to use constants as they are shorter than <code>Configure::read('admin')</code> etc. But Configure would work just as fine.</p>

<p>Then we need to decide whether we use single role (cake default) or multi role Authorization. I usually always use multi-roles. Therefore the default case for the Auth class is exactly this.
The session then contains:
<pre class="php" style="font-family:monospace;">Auth<span style="color: #339933;">.</span>User<span style="color: #339933;">.</span>Role <span style="color: #009900;">&#40;</span>with Role being an <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> of role ids<span style="color: #009900;">&#41;</span></pre>
If you use single roles, you&#8217;re Session array should look like this:
<pre class="php" style="font-family:monospace;">Auth<span style="color: #339933;">.</span>User<span style="color: #339933;">.</span>role&#95;id <span style="color: #009900;">&#40;</span>with role&#95;id being the single role we want to check against<span style="color: #009900;">&#41;</span></pre>
In this case you should set the following constant manually in your bootstrap:
<pre class="php" style="font-family:monospace;"><a href="http://www.php.net/define"><span style="color: #990000;">define</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'USER&#95;ROLE&#95;KEY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'role&#95;id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></p>

<h3>&#8220;Former&#8221; usage</h3>

<p>For comparison I will outline the manual and &#8220;outdated&#8221; way of authorization first:
<pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># we want to make sure that piece is only visible to admins and moderators
</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;">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&#95;id'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> ROLE&#95;ADMIN <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&#95;id'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> ROLE&#95;MOD<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># or with multi-role
</span><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/in&#95;array"><span style="color: #990000;">in&#95;array</span></a><span style="color: #009900;">&#40;</span>ROLE&#95;ADMIN<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#41;</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: #009900;">&#41;</span> <span style="color: #339933;">||</span> <a href="http://www.php.net/in&#95;array"><span style="color: #990000;">in&#95;array</span></a><span style="color: #009900;">&#40;</span>ROLE&#95;MOD<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#41;</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: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></p>

<p>Quite a lot to write&#8230;</p>

<p>Note: This also only works in controller/component and view/helper scope. You would have to use the static CakeSession::read() in order to make this work in the model/behavior one etc.</p>

<h3>Usage</h3>

<p>Now the fun part. The wrapper class can be found in the <a href="https://github.com/dereuromark/tools/blob/2.0/Lib/Auth.php">Tools Plugin</a>.</p>

<p><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Same thing as above
</span><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Auth<span style="color: #339933;">::</span><span style="color: #004000;">hasRoles</span><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>ROLE&#95;ADMIN<span style="color: #339933;">,</span> ROLE&#95;MOD<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>
Now isn&#8217;t that nicer to write and read?</p>

<p>The default case is that if one of the roles is matched it will return true right away. If you want to connect them with AND instead of OR, you need to make the second param <code>false</code>:
<pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># This only passed if the user has both roles!
</span><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Auth<span style="color: #339933;">::</span><span style="color: #004000;">hasRoles</span><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>ROLE&#95;ADMIN<span style="color: #339933;">,</span> ROLE&#95;MOD<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></p>

<p>If we only want to check against a single role we could also use the shorthand:
<pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Auth<span style="color: #339933;">::</span><span style="color: #004000;">hasRole</span><span style="color: #009900;">&#40;</span>ROLE&#95;MOD<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></p>

<h3>Advanced usage</h3>

<p>You can also pass in the roles you want to check against. This can be useful if you want to check somebody else&#8217;s roles (and not your session roles). This can come in handy in CLI (command line / shell) environment and also in the admin backend.</p>

<p><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Auth<span style="color: #339933;">::</span><span style="color: #004000;">hasRole</span><span style="color: #009900;">&#40;</span>ROLE&#95;MOD<span style="color: #339933;">,</span> <span style="color: #000088;">$rolesOfThisUser</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre>
And
<pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Auth<span style="color: #339933;">::</span><span style="color: #004000;">hasRoles</span><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>ROLE&#95;MOD<span style="color: #339933;">,</span> ROLE&#95;USER<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rolesOfThisUser</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></p>

<h3>And there is more</h3>

<p>There are also some convenience methods available.</p>

<p>Instead of <code>$uid = $this-&gt;Session-&gt;read('Auth.User.id')</code> you can just write
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$uid</span> <span style="color: #339933;">=</span> Auth<span style="color: #339933;">::</span><span style="color: #004000;">id</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;">// anywhere in your application</span></pre></p>

<p>The roles can be fetched like this:
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$myRoles</span> <span style="color: #339933;">=</span> Auth<span style="color: #339933;">::</span><span style="color: #004000;">roles</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;">// string in single-role and array in multi-role context</span></pre></p>

<p>Last but not least the user data:
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> Auth<span style="color: #339933;">::</span><span style="color: #004000;">user</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;">// complete user array</span>
<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> Auth<span style="color: #339933;">::</span><span style="color: #004000;">user</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string: current username</span>
<span style="color: #339933;">...</span></pre></p>

<h3>Final notes</h3>

<p>Although this wrapper can be used about anywhere in your application with ease does not mean one should do that.
Try to avoid using the auth (and therefore session) data in the model layer, for instance. Those should be kept state-less.
But I also know that there are cases where it is pretty convenient to ignore this warning <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<h4>Disclaimer</h4>

<p>Just for clarification: This class does not provide you with the Authentication or Authorization. This has to be up and running already. It is only a wrapper to check user roles a more efficient and cleaner way.</p>

<p>So if you need to setup a fresh authentication, you can just use the cake <a href="http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authentication-objects">Form authenticate</a> for example. If you want multi-role authorization you would want to throw in some additional spices:
You would need to write the Role array to your session upon successful login for the authorization module to work. See the above notes on how the session data array should look like.</p>

<p>For a new authorization take a look at my <a href="http://www.dereuromark.de/2011/12/18/tinyauth-the-fastest-and-easiest-authorization-for-cake2/">TinyAuth</a>. It can handle single and multi role auth and those classes work well together (yeah &#8211; they have been designed to do that, of course^^).</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1836&amp;md5=53f7df72c5993150558ff4738d736fd3" 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/04/07/auth-inline-authorization-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=dereuromark&amp;popout=1&amp;url=http%3A%2F%2Fwww.dereuromark.de%2F2012%2F04%2F07%2Fauth-inline-authorization-the-easy-way%2F&amp;language=de_DE&amp;category=text&amp;title=Auth+%26%238211%3B+inline+authorization+the+easy+way&amp;description=I+wrote+a+wrapper+class+to+make+inline+authorization+easier.+Often+times+you+want+to+check+on+certain+roles+inside+an+action+or+view+and+depending+on+the+result+display...&amp;tags=ACL%2CAuth%2CAuthentication%2CAuthorization%2Ccake2.x%2CCakePHP%2CRoles%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Helper? Component? Lib? CakePHP2</title>
		<link>http://www.dereuromark.de/2012/04/03/helper-component-lib-cakephp2/</link>
		<comments>http://www.dereuromark.de/2012/04/03/helper-component-lib-cakephp2/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 23:43:25 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Behavior]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Helper]]></category>
		<category><![CDATA[Lib]]></category>
		<category><![CDATA[Plugin]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1806</guid>
		<description><![CDATA[Note: For 1.3 see the old article on this subject. I want to outline some ideas on how to chose the appropriate class type if you want to add some additional feature to your cake app. For a beginner it might be difficult to decide what to use in which case. Hope this article helps [...]]]></description>
			<content:encoded><![CDATA[<p>Note: For 1.3 see the <a href="http://www.dereuromark.de/2010/06/26/helper-component-lib/">old article on this subject</a>.</p>

<p>I want to outline some ideas on how to chose the appropriate class type if you want to add some additional feature to your cake app.
For a beginner it might be difficult to decide what to use in which case. Hope this article helps to clarify things.
Feel free to comment on this below.</p>

<h3>Overview</h3>

<ul>
<li>bootstrap functions (most generic methods &#8211; no class context)</li>
<li>Lib (most generic class)</li>
<li>Helper (view level)</li>
<li>Component (controller level)</li>
</ul>

<p>And of course the base classes: Datasource, Model and Controller (and View).</p>

<h3>Level of Independence</h3>

<p>We need to ask ourselves if this feature needs to interact with other cake elements, like controller, some components, models, &#8230;</p>

<p>If it needs to save to the session, or if it needs some controller functionality, it will have to be a component.
With initialize(Controller $controllerReference) and startup(Controller $controllerReference) this is very easy to accomplish.</p>

<p>With Cake13 libs have been introduced. Not every piece of &#8220;controller&#8221; code necessarily needs to be component anymore. 
So if you retrieve an RSS feed or get the weather from a weather channel web service you could just make a clean and independent lib class. No need to extend the cake object or even pass the controller reference. Less memory and dependency is a good thing. And its easier to test, anyway.</p>

<p>Helpers are used if the result is in relation to the view &#8211; in other words if it gets printed/echoed right away. If you want to retrieve some web service information and save it to the database use a component instead.</p>

<h3>Database related?</h3>

<p>Often times we need to adjust some model data, we either use a component first and then pass it to the model or we use beforeValidate() and beforeSave() in the model. Same goes for the other direction (from model to controller): afterFind() or a component call afterwards.
This is fine for custom changes. As soon as it could be something useful for several models, it might make sense to build a behavior. The code gets cleaner and your models more powerful.</p>

<p>Examples would be:
&#8220;Last Editor/Last Change&#8221;, &#8220;Geocoding&#8221;, &#8220;Auto-Capitalize first letter of name/title&#8221;, &#8220;Format/Localize Date/Time&#8221;, &#8230;</p>

<h3>Reducing code redundancy</h3>

<p>Now that we have a vague understanding where to use what type of tool, we should think about cutting down the redundancy.
Lets say we use the vendor class &#8220;phpThump&#8221;. We would have to write two wrappers. one for the helper (display images in the view) and one for the component (uploading images and resizing), maybe even for some behavior (validating + uploading + resizing). This wrapper handles default values from Configure::read() and other cake related settings.
In this scenario we should build one single library in <code>/Lib</code>, maybe called <code>PhpthumbLib.php</code>.
Here we put our wrapper with our custom functions.
Then we build a helper (view side) as well as a component or a behavior (controller side). They will import and use the library file. This is a cleaner approach because changes in the library class will be available in all files it is used in.
Bonus: The main library file is easier to test. And therefore testing the other classes afterwards is easier, too.</p>

<p>Generally speaking, all web services should be some kind of library file (some would make a datasource out of it). It doesn&#8217;t matter then if we use it in components or helpers, because it will fit either way.</p>

<p>A helper in a controller, though, is not really a nice thing.
With Cake2.1 there is no need to use helpers in the controller or model code anymore. All text/number/time helper methods have been moved to the Utility package and can now by used from anywhere within your application in a dry and clean way:
<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;">'CakeNumber'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Utility'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$myValueInPercent</span> <span style="color: #339933;">=</span> CakeNumber<span style="color: #339933;">::</span><span style="color: #004000;">toPercentage</span><span style="color: #009900;">&#40;</span><span style="color:#800080;">45.12</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// or dynamically - but statically is easier in this case</span></pre></p>

<h3>Plugin or not?</h3>

<p>If your feature is not site-specific but very generic it probably makes sense to build a plugin.
This way all other apps can easily use the same plugin. Additionally, test cases, assets etc are all combined in one folder &#8211; clean and extendable.</p>

<p>Examples:
A bookmark helper usually can be used in several apps, whereas a custom helper with two functions for this one app will not be very useful anywhere else.</p>

<h3>Usage of those resources</h3>

<p>For <strong>components</strong>, add it to the controller in order to use it in the corresponding actions:
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</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;">'MyC'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># file is in /APP/controllers/components named MyCComponent.php</span></pre>
And in one of the controller&#8217;s actions:
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">MyC</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></p>

<p>For <strong>helpers</strong>, add it to the controller in order to use it in the corresponding views:
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</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;">'MyH'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># file is in /APP/View/Helper named MyHHelper.php</span></pre>
And in one of the controller&#8217;s views:
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">MyH</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></p>

<p><strong>Libs</strong> can be used everywhere &#8211; include them at runtime:
<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;">'MyL'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Lib'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># file is in /APP/Lib/ named MyL.php
</span><span style="color: #000088;">$MyL</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyL<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$MyL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
Possible in controllers, components, behaviors, view, helpers, elements and everything else.
They are the most generic classes you can create.</p>

<p>Sidenote: I like to keep those files appended with a suffix, as well (<code>Lib</code> to avoid collisions with other classes or core classes):
<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;">'MyLLib'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Lib'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># file is in /APP/Lib/ named MyLLib.php
</span><span style="color: #000088;">$MyL</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyLLib<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
But that is just my personal convention.</p>

<p>Also note that you are encouraged in 2.x to group your lib classes in packages. So if you have some Utility Helper of your own, you might want to create a subfolder for it (you can use the core folder names or your own naming scheme):
<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;">'MyUtilityLib'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Utility'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># file is in /APP/Lib/Utility/ named MyUtilityLib.php
</span><span style="color: #000088;">$MyL</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyLLib<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
Same with plugins:
<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;">'UrlCacheManagerLib'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tools.Routing'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># file is in /APP/Plugin/Tools/Lib/Routing/ named UrlCacheManagerLib.php
</span><span style="color: #000088;">$UrlCacheManager</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UrlCacheManagerLib<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></p>

<p>Behaviors and other elements are used similar to the above.</p>

<p>For Plugins simply add the plugin name: &#8220;Text&#8221; becomes &#8220;Plugin.Text&#8221; etc</p>

<p><b>bootstrap functions</b>:
If those functions are so generic that you want to use them like h() or env() you can define them in your bootstrap. But be aware that this can lead to chaos if you do that for too many things.</p>

<h3>Hacks for special use cases</h3>

<p>Sometimes we need to break MVC in order to avoid redundance (and stay DRY).
If a helper has to be available in a controller we need to manually include it then at runtime:
<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;">'MySpecialHelper'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Helper'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
App<span style="color: #339933;">::</span><span style="color: #004000;">uses</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'View'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'View'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$MySpecialHelper</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MySpecialHelper<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> View<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$myText</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$MySpecialHelper</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$myText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
I want to emphasize that this should only be done if not possible any other way.
What I do in this case: Move the functionality to a Lib class and use it inside the helper. This way we can use the Lib in the controller/model level and the helper is still the convenience access for the view level.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1806&amp;md5=d754b022deb7ef33527ccbd5f902dfb1" 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/04/03/helper-component-lib-cakephp2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=dereuromark&amp;popout=1&amp;url=http%3A%2F%2Fwww.dereuromark.de%2F2012%2F04%2F03%2Fhelper-component-lib-cakephp2%2F&amp;language=de_DE&amp;category=text&amp;title=Helper%3F+Component%3F+Lib%3F+CakePHP2&amp;description=Note%3A+For+1.3+see+the+old+article+on+this+subject.+I+want+to+outline+some+ideas+on+how+to+chose+the+appropriate+class+type+if+you+want+to+add+some...&amp;tags=Behavior%2CComponent%2CHelper%2CLib%2CPlugin%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Introducing the EmailLib</title>
		<link>http://www.dereuromark.de/2012/03/30/introducing-the-emaillib/</link>
		<comments>http://www.dereuromark.de/2012/03/30/introducing-the-emaillib/#comments</comments>
		<pubDate>Fri, 30 Mar 2012 12:03:20 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Cake2]]></category>
		<category><![CDATA[Email]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1788</guid>
		<description><![CDATA[This is a good example of how useful it can be to even extend some basic core classes to enhance them and fit them to your needs. In this case we extend the CakeEmail Utility Lib class and add some fixes as well as some enhancements. The full list can be obtained from the documentation [...]]]></description>
			<content:encoded><![CDATA[<p>This is a good example of how useful it can be to even extend some basic core classes to enhance them and fit them to your needs.</p>

<p>In this case we extend the CakeEmail Utility Lib class and add some fixes as well as some enhancements.
The full list can be obtained from the documentation of the class itself which can be found in the <a href="https://github.com/dereuromark/tools/blob/2.0/Lib/EmailLib.php">Tools Plugin</a>.</p>

<h3>Usage</h3>

<p>First things first: How will it be used? Not so much different from the core class. But we don&#8217;t need to set the defaults (as the wrapper utility takes care of that). The idea is to make it as DRY (don&#8217;t repeat yourself) as possible.
So it is just:
<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;">'EmailLib'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tools.Lib'</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;">Email</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EmailLib<span style="color: #009900;">&#40;</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;">Email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">to</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</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;">Email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">replyTo</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;">'Config.admin&#95;email'</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;">//optional</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">subject</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'...'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">viewVars</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/compact"><span style="color: #990000;">compact</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
As you can see the from part is also set automatically (with the <code>Configure::read('Config.admin_email')</code> setting).
The replyTo part is optional but very handy if you want to send emails to users in have of other users (then the reply to part would be set to the other user&#8217;s email address). You should never set &#8220;from&#8221; to anything but your own mail server to avoid your emails being marked as spam.</p>

<p>It is also a good idea to store all site wide email addresses in your Configure, as well. In the above example the admin email + name for the optional replyTo part. Never write those addresses in your code &#8211; it will end up redundant at some point and lead to problems.</p>

<h3>Requirements</h3>

<p>You should set the following config values in Configure:
<pre class="php" style="font-family:monospace;">Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Config.admin&#95;email'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'youremail'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Config.admin&#95;emailname'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yourname'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
There are some more optional Config settings. See the code for details.</p>

<h3>Main improvements</h3>

<p>I will outline only the most important ones which made me use this wrapper class in all 10+ projects for a clean and easy email functionality.</p>

<h4>Extended Logging</h4>

<p>With <code>report=&gt;true</code> you get quite verbose log output saved to <code>email</code> (to, from, subject), <code>email_trace</code> (complete email content) and <code>email_error</code> for SMTP and other errors.</p>

<h4>No mails in debug mode</h4>

<p>If you are in debug mode the emails get only saved to the above <code>email_trace.log</code> &#8211; they will not actually be sent.
This is very helpful in development (no accidental emails to live customers etc).</p>

<p>Note: This requires the <a href="https://github.com/dereuromark/setup/blob/master/Lib/BaseEmailConfig.php">BaseEmailConfig of the Setup plugin</a> (you can also copy and paste that piece of code, of course).
The setup for your <code>APP/Config/email.php</code> would then be:
<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;">'BaseEmailConfig'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Setup.Lib'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> EmailConfig <span style="color: #000000; font-weight: bold;">extends</span> BaseEmailConfig <span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span></pre>
It also allows you to set your Email configs more dynamically (using Configure::read/write) &#8211; see &#8220;other improvements&#8221; below.</p>

<h4>Embedded Attachments</h4>

<p>This will automatically set the contentId and mimeType of the file. This is necessary for embedded attachments.
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addEmbeddedAttachment</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/some/file.ext'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Some custom name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></p>

<h4>Blob Attachments &#8211; as real attachment</h4>

<p>This will automatically set the mimeType of the file (using the filename you provide).
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addBlobAttachment</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$somePngFileBlobContent</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'my&#95;filename.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></p>

<h4>Embedded Blob Attachments</h4>

<p>This will also automatically set the mimeType of the file as above. But it will set the contentId, as well. This is necessary for embedded attachments.
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addEmbeddedBlobAttachment</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$somePngFileBlobContent</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'my&#95;filename.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></p>

<h4>No Exceptions thrown outside the wrapper</h4>

<p>This is quite important because usually no one wraps his email sending functionality with try-catch-blocks.
The wrapper catches those exceptions and logs them and will simply return false.</p>

<h4>Other improvements</h4>

<p>There is more, of course. Like the possibility to set the password and other security sensitive information via non-version-controlled config file (<code>Configure::read/write</code>). But those a minor things that will probably only be used by me.
Or the possibility to override the <code>X-Mailer</code> header.</p>

<p>Example on how to set the configs dynamically via config:
<pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// only the pwd</span>
Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Email.Pwd.default'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mypwd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// complete email config</span>
Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Mail.use&#95;smtp'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Mail.smtp&#95;username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Mail.smtp&#95;password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pwd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">...</span></pre></p>

<h3>Main fixes</h3>

<p>The core class has a <a href="http://cakephp.lighthouseapp.com/projects/42648/tickets/2698-cakeemail-messageid-in-cli-environment">problem with valid message ids</a> in shell environment as of cake2.1 and will probably be fixed in 2.2.
Until then the wrapper also takes care of that.</p>

<p>You can just set the domain manually from the CLI to &#8220;simulate&#8221; the environment you are under:
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">domain</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'www.mydomain.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
This will make your Emails use this domain instead of an empty string in your messageId.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1788&amp;md5=b68a8defb9db7c2b5961d134faf53741" 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/03/30/introducing-the-emaillib/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=dereuromark&amp;popout=1&amp;url=http%3A%2F%2Fwww.dereuromark.de%2F2012%2F03%2F30%2Fintroducing-the-emaillib%2F&amp;language=de_DE&amp;category=text&amp;title=Introducing+the+EmailLib&amp;description=This+is+a+good+example+of+how+useful+it+can+be+to+even+extend+some+basic+core+classes+to+enhance+them+and+fit+them+to+your+needs.+In+this+case...&amp;tags=Cake2%2CEmail%2Cblog" type="text/html" />
	</item>
		<item>
		<title>E_STRICT compliance in Cake</title>
		<link>http://www.dereuromark.de/2012/03/06/e_strict-compliance-in-cake/</link>
		<comments>http://www.dereuromark.de/2012/03/06/e_strict-compliance-in-cake/#comments</comments>
		<pubDate>Tue, 06 Mar 2012 01:47:41 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[E_STRICT]]></category>
		<category><![CDATA[PHP5]]></category>
		<category><![CDATA[Typehinting]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1752</guid>
		<description><![CDATA[In PHP5.4 E_STRICT will be automatically be part of E_ALL. So it might be a good idea to get your code E_STRICT ready. I have to say: I never bothered myself. Always used E_ALL &#38; ~E_STRICT (the second part will now be necessary in 5.4 if you plan on skipping the E_STRICT part) and didn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>In PHP5.4 E_STRICT will be automatically be part of E_ALL. So it might be a good idea to get your code E_STRICT ready.
I have to say: I never bothered myself. Always used <code>E_ALL &amp; ~E_STRICT</code> (the second part will now be necessary in 5.4 if you plan on skipping the E_STRICT part) and didn&#8217;t bother about those notices.</p>

<p>With my recent <a href="https://github.com/cakephp/cakephp/pull/521">contribution to the 2.1 core</a> pretty much all of my helpers, components, behaviors, models, &#8230; are now finally E_STRICT compliant. Which does not only make me happy, it also makes me think if I can drop my personal E_STRICT boycott now.</p>

<p>Most of those notices resulted out of incorrect method declarations. They have to be of the same type as the parent (some type, same number of params, &#8230;) which sometimes can get really annoying if you want to override a parent method.
Depending on the specific case you can either use a separate method which then calls the original parent method or you can try to use composition (instead of inheritance).
And if worst comes to worst: with 5.4 we could now use traits to overcome that obstacle.</p>

<p>Note: If you want to test your script with E_STRICT in PHP &lt;= 5.3 you need to change your core.php error level:
<pre class="php" style="font-family:monospace;">Configure<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error'</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;">'handler'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ErrorHandler::handleError'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'level'</span> <span style="color: #339933;">=&gt;</span>  <span style="color: #009900; font-weight: bold;">E&#95;ALL</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">E&#95;STRICT</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// meaning: E&#95;ALL + E&#95;STRICT (using binary operators)</span>
    <span style="color: #0000ff;">'trace'</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></pre></p>

<h3>How does a overriden method now look like?</h3>

<p>It always a good idea to first take a look at the parent class your class inherits from. The method declaration can then easily be copy-and-pasted. This way nothing gets accidently swept under the rug.</p>

<p>A side note: You should also make sure that you call the parent method for callbacks. This is often forgotten and can lead to strange events. This parent method could do something important that would otherwise be missing.
<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> initialize<span style="color: #009900;">&#40;</span>Controller <span style="color: #000088;">$controller</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    parent<span style="color: #339933;">::</span><span style="color: #004000;">initialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$controller</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># important call
</span> <span style="color: #666666; font-style: italic;">// one stuff</span>
<span style="color: #009900;">&#125;</span></pre></p>

<h3>Concrete examples</h3>

<p>The controllers are pretty easy. Except for maybe <code>beforeRedirect</code> the other callbacks don&#8217;t have any params:
<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> beforeRedirect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$status</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exit</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: #009900;">&#125;</span></pre>
So if we would override this method, we have to use the same declaration.</p>

<p>Helpers are also straight-forward:
<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> afterRender<span style="color: #009900;">&#40;</span><span style="color: #000088;">$viewFile</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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> beforeLayout<span style="color: #009900;">&#40;</span><span style="color: #000088;">$layoutFile</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></p>

<p>More interesting are the components and behaviors, for example. Part of my contribution was adding the overdue type hinting declarations (until now some classes had them, some don&#8217;t). All classes that use objects should now have them properly outlined in the method declaration:
<pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// a component (for example my Common component)</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> initialize<span style="color: #009900;">&#40;</span>Controller <span style="color: #000088;">$controller</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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> beforeRedirect<span style="color: #009900;">&#40;</span>Controller <span style="color: #000088;">$controller</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$status</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exit</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: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// a behavior (for example my Bitmasked behavior)</span>
<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>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> afterFind<span style="color: #009900;">&#40;</span>Model <span style="color: #000088;">$Model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$results</span><span style="color: #339933;">,</span> <span style="color: #000088;">$primary</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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> afterSave<span style="color: #009900;">&#40;</span>Model <span style="color: #000088;">$model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$created</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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> beforeDelete<span style="color: #009900;">&#40;</span>Model <span style="color: #000088;">$model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cascade</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: #009900;">&#125;</span></pre>
If you don&#8217;t pass on a model it will trigger an error &#8211; which is a good thing. PHP gets more and more type safe this way (and with this closer to languages as JAVA where less mistakes and abuse is possible due to those restrictions).</p>

<p>Hopefully, with those tips, you will be able to get your app E_STRICT compliant. Happy coding! <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<h3>Final tips</h3>

<p>Coders are lazy! We don&#8217;t want to do things manually if we can help it.
It is possible to get some of the necessary changes done via <a href="https://github.com/dereuromark/upgrade">upgrade shell</a>. At least I did it this way. Check it out (<code>cake Upgrade.Correct reference</code>) or write your own shell commands. The huge advantage: Once written you can update as many apps as you want with a single click.</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1752&amp;md5=716d73f03cebc6d08926c3a3e57b927e" 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/03/06/e_strict-compliance-in-cake/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=dereuromark&amp;popout=1&amp;url=http%3A%2F%2Fwww.dereuromark.de%2F2012%2F03%2F06%2Fe_strict-compliance-in-cake%2F&amp;language=de_DE&amp;category=text&amp;title=E_STRICT+compliance+in+Cake&amp;description=In+PHP5.4+E_STRICT+will+be+automatically+be+part+of+E_ALL.+So+it+might+be+a+good+idea+to+get+your+code+E_STRICT+ready.+I+have+to+say%3A+I+never+bothered...&amp;tags=E_STRICT%2CPHP5%2CTypehinting%2Cblog" type="text/html" />
	</item>
		<item>
		<title>PHP5.4 is out</title>
		<link>http://www.dereuromark.de/2012/03/03/php5-4-is-out/</link>
		<comments>http://www.dereuromark.de/2012/03/03/php5-4-is-out/#comments</comments>
		<pubDate>Sat, 03 Mar 2012 01:24:59 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP5.4]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1744</guid>
		<description><![CDATA[In case you haven&#8217;t heard yet (pretty much impossible, isn&#8217;t it): PHP5.4 has been released. In a previous post I already pointed out the possibilities for traits &#8211; although I am not quite sure yet how it could benefit current cake projects. This article describes some more news and features regarding this new release. I [...]]]></description>
			<content:encoded><![CDATA[<p>In case you haven&#8217;t heard yet (pretty much impossible, isn&#8217;t it): <a href="http://php.net/releases/5_4_0.php">PHP5.4 has been released</a>.</p>

<p>In a <a href="http://www.dereuromark.de/2011/12/31/traits-php5-4/">previous post</a> I already pointed out the possibilities for traits &#8211; although I am not quite sure yet how it could benefit current cake projects.</p>

<p><a href="http://php.webtutor.pl/en/2011/09/27/whats-new-in-php-5-4-a-huge-list-of-major-changes/">This article</a> describes some more news and features regarding this new release.</p>

<p>I certainly won&#8217;t use any of the &#8220;syntactic sugar&#8221; like short array syntax anytime soon. It would just break with the current way of writing arrays &#8211; and then one would end up with two different syntaxes in the same app or even class.
But some of new features (besides the traits) sure look promising!</p>

<p>What will you be using? Or is there &#8211; especially regarding cake &#8211; no real use for any of those new features?</p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1744&amp;md5=cdf851ecac064a60e2412efa894b555b" 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/03/03/php5-4-is-out/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=dereuromark&amp;popout=1&amp;url=http%3A%2F%2Fwww.dereuromark.de%2F2012%2F03%2F03%2Fphp5-4-is-out%2F&amp;language=de_DE&amp;category=text&amp;title=PHP5.4+is+out&amp;description=In+case+you+haven%26%238217%3Bt+heard+yet+%28pretty+much+impossible%2C+isn%26%238217%3Bt+it%29%3A+PHP5.4+has+been+released.+In+a+previous+post+I+already+pointed+out+the+possibilities+for+traits+%26%238211%3B+although+I...&amp;tags=PHP5.4%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Some new crazy CakePHP tricks</title>
		<link>http://www.dereuromark.de/2012/03/01/some-new-crazy-cakephp-tricks/</link>
		<comments>http://www.dereuromark.de/2012/03/01/some-new-crazy-cakephp-tricks/#comments</comments>
		<pubDate>Thu, 01 Mar 2012 09:20:31 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Autofocus]]></category>
		<category><![CDATA[Cake2]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JS-Fallback]]></category>
		<category><![CDATA[Placeholder]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.dereuromark.de/?p=1721</guid>
		<description><![CDATA[Crazy &#8211; because these tips are not used that often probably. But for some rare occasions they might come in handy. Setting additional attributes for some select options You might want set some option elements to a specific class. The FormHelper::select() is powerful enough to support this: // normal options $options = array&#40;1=&#62;'One', 2=&#62;'Two', 3=&#62;'Three'&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>Crazy &#8211; because these tips are not used that often probably. But for some rare occasions they might come in handy.</p>

<h3>Setting additional attributes for some select options</h3>

<p>You might want set some option elements to a specific class. The FormHelper::select() is powerful enough to support this:
<pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// normal options</span>
<span style="color: #000088;">$options</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: #cc66cc;">1</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'One'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Two'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Three'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// advanced (second elements has a class &quot;extra&quot;)</span>
<span style="color: #000088;">$options</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: #cc66cc;">1</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'One'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Two'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'value'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'class'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'extra'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Three'</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;">'test'</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;">'select'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'options'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
The result:
<pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input select&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/label.html"><span style="color: #000000; font-weight: bold;">label</span></a> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ModelTest&quot;</span>&gt;</span>Test<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/label.html"><span style="color: #000000; font-weight: bold;">label</span></a>&gt;</span>
    <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/select.html"><span style="color: #000000; font-weight: bold;">select</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;data[Model][test]&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ModelTest&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span>&gt;</span>One<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>
        <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;extra&quot;</span>&gt;</span>Two<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>
        <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;3&quot;</span>&gt;</span>Three<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/select.html"><span style="color: #000000; font-weight: bold;">select</span></a>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span></pre></p>

<p>You might want to check <a href="http://www.electrictoolbox.com/style-select-optgroup-options-css/">this site here</a> if the styles you want to apply work in all browsers. Usually only some styles like <code>background-color</code> are apply-able cross-browser.</p>

<h3>Autofocus</h3>

<p>Not so much CakePHP as maybe <a href="http://www.w3schools.com/html5/att_input_autofocus.asp">HTML5</a> &#8211; but pretty useful for login forms:
<pre class="php" style="font-family:monospace;"><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;">'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;">'autofocus'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'autofocus'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
All new browsers support it out of the box. The user can start typing right away (no additional click necessary).</p>

<h3>More HTML5 out-of-the-box</h3>

<p>In general HTML5 is great, but still not fully supported by all browsers &#8211; or users just don&#8217;t update to a new version.
So we shouldn&#8217;t completely rely on those features. But where &#8211; as the above autofocus &#8211; the absence in unsupportive browsers doesn&#8217;t bother anyone we can easily apply those features for users with modern browsers.</p>

<p>Additionally, we can use fallback-js to &#8220;simulate&#8221; the behavior if not natively supported. I put together a well-working <a href="https://github.com/dereuromark/tools/blob/2.0/webroot/js/jquery.html5autofallback.js">HTML5-fallback script</a> for jquery. It checks if the browser supports a certain feature and if not it creates a JS workaround.</p>

<p>I bet there are lots of other similar implementations. The important part for those placeholders is that on submit the content needs to be cleared again. The placeholders must not be posted (otherwise your validation rules might not work as expected anymore). I used the <code>$('form').live('submit', function() {}</code> event to clear the forms in time.</p>

<p><pre class="php" style="font-family:monospace;"><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;">'url'</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;">'placeholder'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'http://'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>
All these snippets work in all browsers. In those modern ones a little bit faster. But that&#8217;s about all. You can already use it without any worries. And some day you can just turn of the fallback.</p>

<h3 id="structure-your-controllers">Structure your controllers</h3>

<p>You don&#8217;t have to put all 40ish actions that are related to the User model in a single UsersController. Divide them to keep things clean and clear. So for me this worked pretty well:</p>

<ul>
<li>AccountController (login, logout, register, change_pw, edit, &#8230;): <code>$uses = array('User')</code></li>
<li>MembersController (index, search, profile, mail, friend, &#8230;, admin backend actions): <code>$uses = array('User')</code></li>
<li>OverviewController (index, admin index, &#8230; &#8211; specially routed): <code>$uses = array('User')</code></li>
</ul>

<p>As you can see they all use the user model but they separate the actions by category. This way the file stays kinda short and is better understandable in the long run. Imagine all those actions in a single controller. No way <img src='http://www.dereuromark.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<h3>Controllers without models</h3>

<p>Until today you coudn&#8217;t just set <code>$uses</code> to <code>false</code> in Cake2. What used to work in 1.3 will fail hard in 2.x. It was a pretty annoying problem I <a href="http://cakephp.lighthouseapp.com/projects/42648/tickets/2399">reported</a> right away (but it needed a <a href="http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2590">second one</a> to actually get things into motion). If you just used <code>isset($this-&gt;Model)</code> it would try to load a non-existent model and die. The current head now contains the new <a href="https://github.com/cakephp/cakephp/commit/1ddcde7c864801625d5254c920a83c0ee0d4c3c8">fixed version</a> which works now the way one would expect it to work:</p>

<ul>
<li><code>true</code> Use the default inflected model name.</li>
<li><code>array()</code> Use only models defined in the parent class.</li>
<li><code>false</code> Use no models at all, do not merge with parent class either.</li>
<li><code>array('Post', 'Comment')</code> Use only the Post and Comment models. Models will also be merged with the parent class.</li>
</ul>

<p>The default value is <code>true</code>.</p>

<p>So basically, if we don&#8217;t want to use any models (or at least no default model):
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> TestController <span style="color: #000000; font-weight: bold;">extends</span> AppController <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$uses</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></p>
 <p><a href="http://www.dereuromark.de/?flattrss_redirect&amp;id=1721&amp;md5=ab434a93e30ffcc96f515c52e89b72b6" 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/03/01/some-new-crazy-cakephp-tricks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=dereuromark&amp;popout=1&amp;url=http%3A%2F%2Fwww.dereuromark.de%2F2012%2F03%2F01%2Fsome-new-crazy-cakephp-tricks%2F&amp;language=de_DE&amp;category=text&amp;title=Some+new+crazy+CakePHP+tricks&amp;description=Crazy+%26%238211%3B+because+these+tips+are+not+used+that+often+probably.+But+for+some+rare+occasions+they+might+come+in+handy.+Setting+additional+attributes+for+some+select+options+You+might...&amp;tags=Autofocus%2CCake2%2CHTML5%2CJS-Fallback%2CPlaceholder%2Cselect%2CTips%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>

