<?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>Richard Willis-Owen</title>
	<atom:link href="http://www.willis-owen.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.willis-owen.co.uk</link>
	<description>PHP MySQL Web Developer</description>
	<lastBuildDate>Tue, 06 Dec 2011 16:42:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>CakePHP 2.0 paginate on related model</title>
		<link>http://www.willis-owen.co.uk/2011/12/cakephp-2-0-paginate-on-related-model/</link>
		<comments>http://www.willis-owen.co.uk/2011/12/cakephp-2-0-paginate-on-related-model/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 16:42:04 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.willis-owen.co.uk/?p=344</guid>
		<description><![CDATA[Just a quick note on this one as it took a while to discover how to it. Say you have a relationship between two tables and you want to be able to use the built in pagination helper to generate &#8230; <a href="http://www.willis-owen.co.uk/2011/12/cakephp-2-0-paginate-on-related-model/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Just a quick note on this one as it took a while to discover how to it.</p>
<p>Say you have a relationship between two tables and you want to be able to use the built in pagination helper to generate sort links on the content in the related table.<br />
One way of doing this is to add virtual fields to bring the related column(s) into the Model.</p>
<p>In your controller:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">virtualFields</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'User.name'</span><span style="color: #339933;">;</span></pre></div></div>

<p>then in your view you can generate a sort link for this field with:</p>

<div class="wp_syntax"><div class="code"><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;">Paginator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.willis-owen.co.uk/2011/12/cakephp-2-0-paginate-on-related-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP validation on a Search Form</title>
		<link>http://www.willis-owen.co.uk/2011/11/cakephp-validation-on-a-search-form/</link>
		<comments>http://www.willis-owen.co.uk/2011/11/cakephp-validation-on-a-search-form/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 17:27:03 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.willis-owen.co.uk/?p=338</guid>
		<description><![CDATA[I wanted to validate the fields on a search form (e.g. date, price) like the add/edit forms work when you bake or use the scaffolding. Why is this a problem. CakePHP has great built in validation but it is tied &#8230; <a href="http://www.willis-owen.co.uk/2011/11/cakephp-validation-on-a-search-form/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I wanted to validate the fields on a search form (e.g. date, price) like the add/edit forms work when you bake or use the scaffolding.</p>
<h3>Why is this a problem.</h3>
<p>CakePHP has great built in validation but it is tied into saving data.<br />
If you have a search form on your website, nothing needs to be saved and there probably isn&#8217;t even a suitable Model anyway.<br />
Another issue is that I wanted it to work where my index view contains the search form and this posts to the results action. The results action processes the search and the view displays the search results. If there&#8217;s a validation error you need to go back to the index view.</p>
<p>I did consider just using jQuery to validate the form contents before it went up to the server &#8211; but I&#8217;m sure you all know that it is poor practice to just use client side validation.</p>
<p>To validate like the add or edit scaffolding the view needs to submit to its own action just like they do.<br />
However that would mean the search results would also be displayed within the same view.</p>
<ol>
<li>I could run the query and then render a different view (but then the URL won&#8217;t change).</li>
<li>I could store the search params in a session and redirect to the results view, then retrieve the search params, run the search and display it.</li>
</ol>
<p>I began to worry how well pagination would work with either of these options.</p>
<p>I decided to try this with the way I wanted it to work &#8211; the search view submitting to a different action which displays the results, then if invalid I just needed a way of displaying the search form again and highlighting the errors.</p>
<p>The trick is to store validation messages and posted form data in Sessions and redirect back to the search form.</p>
<p>First I considered reading the Session in the view and injecting into CakePHP variables.<br />
The code below shows how to add validation information within a view:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validationErrors</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Validation message text here'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validationErrors</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Price should be &lt; 100'</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;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Form field data in here'</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;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">123</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>In practice it is easier to inject those messages in the Controller.<br />
Many thanks for this blog article which showed me how to do it:<br />
<a href='http://www.jamesfairhurst.co.uk/posts/view/validating_cakephp_data_from_another_model/'>http://www.jamesfairhurst.co.uk/posts/view/validating_cakephp_data_from_another_model/</a></p>
<p>In the controller action which receives form input you do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'search_text'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validationErrors</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'search_text'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'You must provide search text.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validationErrors</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'resort_id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'You must provide a price.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validationErrors</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Category'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #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;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CategoryErrors'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validationErrors</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This checks for errors, stores the errors and data in sessions and redirects back to the search form.</p>
<p>And in the controller action which displays the form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//see if there were any validation problems</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;">check</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   
	<span style="color: #666666; font-style: italic;">// get data the user posted   </span>
	<span style="color: #000088;">$chalet</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
	<span style="color: #666666; font-style: italic;">// get the errors   </span>
	<span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CategoryErrors'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
	<span style="color: #666666; font-style: italic;">// set their data for view   </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;">'Category'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$chalet</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>   
	<span style="color: #666666; font-style: italic;">// set validation errors for view   </span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span> <span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validationErrors</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$errors</span><span style="color: #339933;">;</span>   
	<span style="color: #666666; font-style: italic;">// delete the session data   </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;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Category'</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;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CategoryErrors'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This breaks CakePHP&#8217;s principles of Fat Models and Skinny Controllers (maybe I could move some of this code into Model functions) but I&#8217;d be very interested to see what other ways there are of doing this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willis-owen.co.uk/2011/11/cakephp-validation-on-a-search-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic select box with CakePHP 2.0</title>
		<link>http://www.willis-owen.co.uk/2011/11/dynamic-select-box-with-cakephp-2-0/</link>
		<comments>http://www.willis-owen.co.uk/2011/11/dynamic-select-box-with-cakephp-2-0/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 16:02:44 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.willis-owen.co.uk/?p=332</guid>
		<description><![CDATA[I wanted some auto populating Select boxes in a site I was creating so that when I changed a Category the Subcategories would automatically update. CakePHP can do this pretty easily but it is let down by the documentation as &#8230; <a href="http://www.willis-owen.co.uk/2011/11/dynamic-select-box-with-cakephp-2-0/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I wanted some auto populating Select boxes in a site I was creating so that when I changed a Category the Subcategories would automatically update. CakePHP can do this pretty easily but it is let down by the documentation as there are no examples.</p>
<p>Initially I came up with a version that wrote JSON into a JavaScript variable in the page and then used jQuery to achieve the updating of select elements but I wanted to do this the Cake way which uses AJAX and as little code as possible.</p>
<p>Here is a simplified solution to demonstrate how it can be done.</p>
<p>This assumes a new CakePHP site already configured with a database connection. I used CakePHP 2.0.3 but this may also work with 1.3 (?)</p>
<h2>1. Models</h2>
<p>We need 3 associated tables</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`categories`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`categories`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">VALUES</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'books'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'music'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'electronics'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`posts`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`title`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`subcategory_id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`posts`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`title`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`subcategory_id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">VALUES</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'The title'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'A title once again'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Title strikes back'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`subcategories`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`category_id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`subcategories`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`category_id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">VALUES</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'fiction'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'biography'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'children'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'classical'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'rock'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'jazz'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'camera'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'audio'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'tv'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Bake the 3 Models for these and allow CakePHP to define model associations for you.</p>
<h2>2. Controllers</h2>
<p>Bake a Controller for the Post Model with the default CRUD actions.</p>
<p>While you are at it you will also need to bake the CRUD Views for the Post Controller.</p>
<p>If you browse to the posts index page now you can view the data.<br />
In this example I will add the Category and Subcategory select lists to the Add view, so now we need to change this so that the selection in the second list changes according to the selection in the first list.<br />
This will be done via the Js Helper so make it available at the top of the Posts controller (after the Class declaration) with:</p>

<div class="wp_syntax"><div class="code"><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> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You need a Subcategories Controller with a single action to provide the data via AJAX:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
App<span style="color: #339933;">::</span><span style="color: #004000;">uses</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AppController'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Controller'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> SubcategoriesController <span style="color: #000000; font-weight: bold;">extends</span> AppController <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getByCategory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$category_id</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;">'Post'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'category_id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$subcategories</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Subcategory</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'list'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Subcategory.category_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$category_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'recursive'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>
			<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'subcategories'</span><span style="color: #339933;">,</span><span style="color: #000088;">$subcategories</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;">layout</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ajax'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>3. Views</h2>
<p>The view is a very simple AJAX view that renders the <code>option</code> tags that go within the <code>select</code> tag.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- file path View/Subcategories/get_by_category.ctp --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$subcategories</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;option value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/option&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h2>4. Putting it all together</h2>
<p>In the Post Add view, file path: <code>View/Posts/add.ctp</code> we can finally add the Js methods that make the dynamic updating happen. This is the cryptic bit that I struggled with for a few hours as although the CakePHP documentation outlines all the options there are not any complete examples.<br />
Firstly add a categories select element to the form (and change the order of the elements):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'category_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'subcategory_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Add the following code to the bottom of the view, this uses the Js Helper to create the necessary jQuery to perform the updating:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Js</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#PostCategoryId'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">event</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'change'</span><span style="color: #339933;">,</span> 
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Js</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'controller'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'subcategories'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'action'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'getByCategory'</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'update'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'#PostSubcategoryId'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'async'</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;">'method'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'dataExpression'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'data'</span><span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Js</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">serializeForm</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'isForm'</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;">'inline'</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: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This is saying &#8211; watch the HTML element with Id PostCategoryId for a change. When it changes update the HTML element with Id PostSubcategoryId with the response from <code>subcategories/GetByCategory</code>, the <code>data</code> option is used to send the current value from the initial select element.</p>
<p>Before you leap to test this you need to make changes to the default layout to include jQuery and provide a place for your scripts to be written out.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// file path View/Layouts/default.ctp</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">charset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;title&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$title_for_layout</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/title&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meta</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'icon'</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;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">css</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cake.generic'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/head&gt;
&lt;body&gt;
	&lt;div id=&quot;container&quot;&gt;
		&lt;div id=&quot;header&quot;&gt;
			&lt;h1&gt;Dynamic Select Box Demonstration&lt;/h1&gt;
		&lt;/div&gt;
		&lt;div id=&quot;content&quot;&gt;
&nbsp;
			<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;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">flash</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$content_for_layout</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		&lt;/div&gt;
		&lt;div id=&quot;footer&quot;&gt;
			footer
		&lt;/div&gt;
	&lt;/div&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">element</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sql_dump'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js&quot;&gt;&lt;/script&gt;
	&lt;!-- scripts_for_layout --&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$scripts_for_layout</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;!-- Js writeBuffer --&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">class_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'JsHelper'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Js</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'writeBuffer'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Js</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">writeBuffer</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;">// Writes cached scripts</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Now you can test the application. On changing the category select list, the subcategory one will automatically update to show the relevant options.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willis-owen.co.uk/2011/11/dynamic-select-box-with-cakephp-2-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP 2.0 AJAX sortable list that updates to databases</title>
		<link>http://www.willis-owen.co.uk/2011/11/cakephp-ajax-sortable-list-with-updates/</link>
		<comments>http://www.willis-owen.co.uk/2011/11/cakephp-ajax-sortable-list-with-updates/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 14:19:27 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.willis-owen.co.uk/?p=324</guid>
		<description><![CDATA[It is easy to create an AJAX sortable list with CakePHP 2.0, using jQuery that automatically updates the database, however it is hard to find documentation explaining how to do it. Here is my explanation &#8211; it assumes a basic &#8230; <a href="http://www.willis-owen.co.uk/2011/11/cakephp-ajax-sortable-list-with-updates/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It is easy to create an AJAX sortable list with CakePHP 2.0, using jQuery that automatically updates the database, however it is hard to find documentation explaining how to do it.</p>
<p>Here is my explanation &#8211; it assumes a basic knowledge of CakePHP.</p>
<h2>Step 1: Data</h2>
<p>Lets use a simple categories table which includes a column for <code>sort_order</code>. This will be used to define the order the records are displayed on the page.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`categories`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`sort_order`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<h2>Step 2: CakePHP</h2>
<p>Bake a model, controller and views for this table so that we have some files to work with.<br />
You should be able to go onto your website at http://yoururl/categories and see the default index view.</p>
<h3>Layout</h3>
<p>In order to use the sortable feature, you will need to edit the default layout. If you don&#8217;t already have your own layout you can copy the CakePHP default one (which is used if your own one isn&#8217;t found) from <code>/lib/Cake/View/Layouts/default.ctp</code> and place it in <code>app/View/Layouts/default.ctp</code>.<br />
Edit this so that the writeBuffer method is one of the final lines, just before the close body tag:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Js</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">writeBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>This will create the HTML and JavaScript generated by the Js helper in the view.</p>
<h3>Controller</h3>
<p>At the top of the <code>CategoriesController</code> make the Js helper available to the controller.</p>

<div class="wp_syntax"><div class="code"><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> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I am going to add 2 actions, one to display the sorted categories and one to handle the AJAX requests to update the database.<br />
The first action is called <code>sort</code>, all this does is retrieve all the categories sorted by the sort_order column and send them off to the view.</p>

<div class="wp_syntax"><div class="code"><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> <span style="color: #990000;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categories'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'order'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'sort_order ASC'</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The second action is called <code>reorder</code>.<br />
This will receive the AJAX post and update the categories table. I&#8217;ve commented out a line that can be used for debugging. If uncommented this will write to <code>app/tmp/logs/error.log</code> and is a really good way to see what is happening when you are dealing with AJAX requests.</p>

<div class="wp_syntax"><div class="code"><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> reorder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">saveField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sort_order&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$key</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">//$this-&gt;log(print_r($this-&gt;data,true));</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The code here is just looping through the posted data which will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>Category<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">6</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
        <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>The index is the order of elements after they have been sorted and the value is the id of each element. When saving the data I am adding 1 to the index as the one coming from jQuery is zero based.</p>
<h3>View</h3>
<p>For the sake of simplicity I&#8217;ve included the links to the jQuery and jQuery UI libraries here but in practice you would probably want to put them in the head of your layout.<br />
In the view I have a simple unordered list tag containing the data.<br />
The id of each element is important as this is used by jQuery UI to format the data sent back to the server.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js&quot;&gt;&lt;/script&gt;
&lt;ul id=&quot;my-list&quot;&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$categories</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$category</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;li id='Category_<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$category</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>'&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$category</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/li&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/ul&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Js</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#my-list'</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;">Js</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sortable</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'complete'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'$.post(&quot;/categories/reorder&quot;, $(&quot;#my-list&quot;).sortable(&quot;serialize&quot;))'</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>And that is all there is to it.<br />
You can simply drag and drop any item in the list and behind the scenes the database will be updated with the new sort order.<br />
<a href="http://www.willis-owen.co.uk/wp-content/uploads/2011/11/sort-screenshot.png"><img src="http://www.willis-owen.co.uk/wp-content/uploads/2011/11/sort-screenshot-300x224.png" alt="" title="sort screenshot" width="300" height="224" class="alignnone size-medium wp-image-326" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.willis-owen.co.uk/2011/11/cakephp-ajax-sortable-list-with-updates/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Blog Tutorial with CakePHP Framework</title>
		<link>http://www.willis-owen.co.uk/2011/10/blog-tutorial-with-cakephp-framework/</link>
		<comments>http://www.willis-owen.co.uk/2011/10/blog-tutorial-with-cakephp-framework/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 12:33:24 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.willis-owen.co.uk/?p=301</guid>
		<description><![CDATA[In a follow up to my previous article: blog tutorial with Fat-Free Framework I have tried to make exactly the same application but using the CakePHP Framework &#8211; to contrast using a micro-framework with a fully featured one. CakePHP works &#8230; <a href="http://www.willis-owen.co.uk/2011/10/blog-tutorial-with-cakephp-framework/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In a follow up to my previous article: <a href='/2011/09/blog-tutorial-with-fat-free-framework/'>blog tutorial with Fat-Free Framework</a> I have tried to make exactly the same application but using the CakePHP Framework &#8211; to contrast using a micro-framework with a fully featured one.<br />
CakePHP works with PHP 4 or 5 so this should work on most servers.</p>
<h2>Step 1: Setup</h2>
<p>Download CakePHP from the website. This tutorial uses version 1.3.11.<br />
It runs happily from its own folder in a web site (so you don&#8217;t need to set up a separate virtual site) &#8211; I just used a folder called <code>cake1</code>, with the contents looking like this<br />
<a href="http://www.willis-owen.co.uk/wp-content/uploads/2011/10/cake-1.png"><img src="http://www.willis-owen.co.uk/wp-content/uploads/2011/10/cake-1.png" alt="CakePHP root directory" title="CakePHP root directory" width="287" height="196" class="alignnone size-full wp-image-302" /></a></p>
<h2>Step 2: Bootstrapping</h2>
<p>We&#8217;ll use this section to configure the database connection.<br />
Copy <code>/app/config/database.php.default</code> to <code>database.php</code> in the same folder.<br />
Edit the values in the <code>$default</code> array to match your database.<br />
You should also edit <code>/app/config/core.php</code> and change the values for <code>Security.salt</code> and <code>Security.cipherSeed</code>.<br />
Make the <code>app/tmp</code> folder writable for the webserver by <code>chown -R www-data app/tmp</code>.<br />
As we&#8217;re developing it may be worth disabling caching so in <code>/app/config/core.php</code> so uncomment</p>

<div class="wp_syntax"><div class="code"><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;">'Cache.disable'</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></pre></div></div>

<h2>Step 3: Routing</h2>
<p>With CakePHP a lot of routing happens automagically by putting correctly names files in the right places.<br />
However we need to tell it a route for the homepage of the site.<br />
Edit <code>/app/config/routes.php</code> and comment out all the default route(s), replacing with one to the articles controller.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Router::connect('/', array('controller' =&gt; 'pages', 'action' =&gt; 'index'));</span>
Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'articles'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Step 4: Models</h2>
<p>CakePHP needs the database to follow a naming convention. The table names should be plural so we&#8217;ll use articles and users.<br />
CakePHP will dynamically creates model objects for you if it cannot find corresponding files in <code>/app/models</code>, as this is a tutorial about getting a prototype running quickly we&#8217;ll take advantage of this here.<br />
Here&#8217;s the SQL that will set you up with the 2 tables necessary for this tutorial, it is exactly the same as for my Fat-Free Framework tutorial:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> <span style="color: #ff0000;">`blog`</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_general_ci;
&nbsp;
<span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #ff0000;">`blog`</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`articles`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`timestamp`</span> datetime <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`title`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`summary`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`content`</span> text <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`author`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM  <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`articles`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`timestamp`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`title`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`summary`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`content`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`author`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2011-07-28 02:03:14'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Hello World!'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Mr White'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2011-07-28 02:03:14'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'More Hello World!'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Mr Green'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`password`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`password`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'admin'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'password'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<h2>Step 5: Application Front End</h2>
<p>First create a layout that will be used as a template for the individual views <code>/app/views/layouts/default.ctp</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$title_for_layout</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$content_for_layout</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Doing this will override the default CakePHP layout which means you will loose the nice diagnostic section that tells you what queries are being sent to the database. You can get this back whenever you want by temporarily renaming the layout <code>default.ctp</code> to something like <code>default.html</code></p>
<p>Now the layout is in place, create a file called <code>articles_controller.php</code> inside <code>/app/controllers</code> with the code below. The index function provides the logic for the homepage and tells it to retrieve all articles and the <code>set</code> command passes the data to the view.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> ArticlesController <span style="color: #000000; font-weight: bold;">extends</span> AppController <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Html'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Articles'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'articles'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Add in another function to deal with the detail view which gets all the data for an individual record.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> view<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'article'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now the logic is in place lets deal with the views. Create a file for the home page <code>/app/views/articles/index.ctp</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;p&gt;Blog Titles&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$articles</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;p&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'articles'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> by <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'author'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
	&lt;p&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'summary'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This is more complicated than the Fat-Free Framework version and begins to show where using a micro framework may save you a lot of typing.<br />
The file for the details view should be saved as <code>/app/views/articles/view.ctp</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;p&gt;Published: <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#93;</span><span style="color: #000000; font-weight: bold;">?&gt;</span> by <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'author'</span><span style="color: #009900;">&#93;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;p&gt;&lt;a href='../'&gt;Back to Homepage&lt;/a&gt;&lt;/p&gt;</pre></div></div>

<p>You can now check this all works by visiting <code>/articles/index</code> with a browser.</p>
<h2>Step 6: Application Back End</h2>
<p>To mimic the previous tutorial I did, the whole admin section needs to accessed under a <code>/admin</code> URL.<br />
This is built into CakePHP and you can achieve it by editing <code>/app/config/core.php</code> and uncommenting</p>

<div class="wp_syntax"><div class="code"><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;">'Routing.prefixes'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can now add functions like <code>admin_index()</code> to a controller and these are only available when using an admin prefix in the URL like <code>/admin/articles</code>.<br />
The admin home page is very similar to the normal home page with some extra links to add/edit/delete blog articles, so the controller method looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> admin_index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'articles'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And the view, saved as <code>/app/views/articles/admin_index.ctp</code> looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;h1&gt;My Blog Administration&lt;/h1&gt;
&lt;p&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Add Article'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'articles'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'admin_add'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Title&lt;/th&gt;
      &lt;th&gt;Date&lt;/th&gt;
      &lt;th&gt;Author&lt;/th&gt;
      &lt;th colspan='2'&gt;Actions&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$articles</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;tr&gt;
      &lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
      &lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
      &lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'author'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
      &lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'articles'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'admin_edit'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
      &lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Delete'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'articles'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'admin_delete'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;/tbody&gt;
&lt;/table&gt;</pre></div></div>

<p>You can now access this via <code>/admin/articles/</code>, we&#8217;ll add the authentication in the next step.</p>
<p>CakePHP uses something called the Flash to communicate temporary messages to the user (don&#8217;t confuse with Adobe Flash).<br />
The following code in a view will display the message if present:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">flash</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>We will add this to the <code>admin_index</code> view under the H1 tag so that messages from the add/edit/delete actions can be displayed.<br />
Also the following code is required to make the component available in the controller:</p>

<div class="wp_syntax"><div class="code"><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> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Session'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note that built into CakePHP is a mechanism for confirming the delete of these records:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Delete'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'admin_delete'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Are you sure you want to delete # %s?'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$article</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This is starting to get complicated by the number of parameters you have to pass this method. I you use this every day then maybe it will become second nature, however for a beginner it can be a little daunting.</p>
<p>Add/Edit/Delete<br />
To keep things simple we will create separate actions and views for the add/edit functionality &#8211; even though there is a lot of similarity between the two.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> admin_add<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Your article has been saved.'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> admin_edit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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: #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;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Your article has been saved.'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> admin_delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The article has been deleted.'</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;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Delete doesn&#8217;t require a view as the action is confirmed by use of the flash. Edit is nearly the same as Add except for a few extra lines to retrieve the record first.<br />
CakePHP uses a method called &#8216;save&#8217; to save changes to the database, but behind the scenes this is going to be an insert or update. If Cake gets passed an id field via the submitted form it assumes you are going to be updating a record.</p>
<p>Thoughts at this stage &#8211; can clearly see the difference between a micro framework and a full size one now, Cake is more complex, there&#8217;s more to learn (much more documentation) and it takes longer.<br />
Now the views of Add and Edit, these are nearly identical except that Edit includes the id field in a hidden variable.<br />
<code>views/articles/admin_add.ctp</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;h1&gt;Add&lt;/h1&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'summary'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'author'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><code>views/articles/admin_edit.ctp</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;h1&gt;Edit&lt;/h1&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Article'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'summary'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'author'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'hidden'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Submit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You can now test this in a browser and should be able to add/edit and delete records.</p>
<h2>Step 7: Using Middleware</h2>
<p>To implement authentication from a database table we use CakePHPs Authentication Component.<br />
This gets messy if you want to do it differently to the CakePHP way &#8211; that means we need to create a new database table with different column names and use that instead.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> users;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> users <span style="color: #66cc66;">&#40;</span>
    id <span style="color: #993333; font-weight: bold;">INTEGER</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
    username <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
    password <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
    <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This requires a users controller called <code>users_controller.php</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> UsersController <span style="color: #000000; font-weight: bold;">extends</span> AppController <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Users'</span><span style="color: #339933;">;</span>    
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$components</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #009900;">&#40;</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;">function</span> logout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And the following 2 changes to <code>articles_controller.php</code>, update the components variable to include the Auth component and add a new function.</p>

<div class="wp_syntax"><div class="code"><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> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Session'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//changed to add Auth</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> beforeFilter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allow</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loginAction</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin'</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;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The beforeFilter function changes some of the defaults for the Auth component, the first line allows the index and view actions to be executed without requiring authentication. Without the second line CakePHP will look for an action called admin_login so this tells it to simply use the login action.<br />
Now we need a view to contain a login form.<br />
<code>views/users/login.ctp</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">flash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As the passwords are stored hashed in the database (which is a good idea) we have a minor annoyance here &#8211; we can&#8217;t insert a row for the admin user into the database manually as we don&#8217;t know what the password field needs to hold.<br />
We can get around this by returning to the CakePHP default layout which contains useful debug information and see what SQL it is using.<br />
Rename <code>app/views/layouts/default.ctp</code> to <code>default.htm</code><br />
Visit /admin/articles and input your chosen username and password (I used &#8216;admin&#8217; and &#8216;password&#8217; for this tutorial) then click submit, in the diagnostic queries section you will see what SQL it tried to use, copy the hashed version of the password and manually insert this into the users database table with your username.<br />
<a href="http://www.willis-owen.co.uk/wp-content/uploads/2011/10/cake2.png"><img src="http://www.willis-owen.co.uk/wp-content/uploads/2011/10/cake2-300x232.png" alt="CakePHP default template showing SQL" title="CakePHP default template showing SQL" width="300" height="232" class="alignnone size-medium wp-image-306" /></a><br />
Now you can rename <code>default.htm</code> back to <code>default.ctp</code> and get your own layout back.</p>
<p>Thoughts &#8211; if you know what you are doing it can be set up with very little coding and it works well.<br />
However it took me about 45 mins to figure it out from the documentation on their website.</p>
<h2>Step 8: Summary</h2>
<p>I guess it should have been obvious from the start but using a bigger framework will lead to more complexity and a steeper learning curve. In exchange for that you gain greater flexibility which aren&#8217;t being taken advantage of with such a simple system developed here. A some really important ones for me are the form helper, validation, built in pagination (with sorting) and custom DataSources. You may be interested in my article on <a href='/2010/02/cakephp-paging-and-sorting-on-a-custom-datasource/'>creating a DataSource to read from a RESTful API</a> which lets you page through and sort the data just as if it were from MySQL.</p>
<p>Rather than putting the whole framework into a zip file, I&#8217;ve just included the files that I created with this tutorial, they all exist within the <code>app</code> folder of the CakePHP installation.</p>
<pre>
controllers
	articles_controller.php
	users_controller.php
views
	articles
		admin_add.ctp
		admin_edit.ctp
		admin_index.ctp
		index.ctp
		view.ctp
	layouts
		default.ctp
	users
		login.ctp
</pre>
<p><a href='http://www.willis-owen.co.uk/wp-content/uploads/2011/10/cake-app-folder.zip'>Download CakePHP app folder</a>.</p>
<p>I&#8217;m going to try this very same blog with CodeIgniter next.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willis-owen.co.uk/2011/10/blog-tutorial-with-cakephp-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blog Tutorial with Fat-Free Framework</title>
		<link>http://www.willis-owen.co.uk/2011/09/blog-tutorial-with-fat-free-framework/</link>
		<comments>http://www.willis-owen.co.uk/2011/09/blog-tutorial-with-fat-free-framework/#comments</comments>
		<pubDate>Fri, 30 Sep 2011 09:25:30 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.willis-owen.co.uk/?p=267</guid>
		<description><![CDATA[I recently read an excellent tutorial on &#8216;Rapid Application Prototyping in PHP Using a Micro Framework&#8216;, this used the Slim micro framework but one thing that bothered me was it required 5 packages before development could start. These were: Slim &#8230; <a href="http://www.willis-owen.co.uk/2011/09/blog-tutorial-with-fat-free-framework/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently read an excellent tutorial on &#8216;<a href="http://net.tutsplus.com/tutorials/php/rapid-application-prototyping-in-php-using-a-micro-framework/">Rapid Application Prototyping in PHP Using a Micro Framework</a>&#8216;, this used the Slim micro framework but one thing that bothered me was it required 5 packages before development could start.<br />
These were: Slim (the micro framework), Slim Extras (additional resources for Slim), Twig (template engine), Idiorm (object-relational mapper) and Paris (Active Record implementation).<br />
It struck me that if you need an extra 4 packages alongside Slim for a basic blog then maybe it is a little too skinny and I set out to find a micro framework that could do the same thing without these dependencies.</p>
<p>I found the <a href="fatfree.sourceforge.net">Fat-Free Framework</a>. It is condensed into a single 55KB file and has a host of features, you can find out about these on their web site so I won&#8217;t repeat it here. Instead I&#8217;ve reproduced the Slim tutorial to create a simple blog site, but using Fat-Free Framework instead.<br />
You will need PHP 5.3 on your server, I used Ubuntu 11.04 for the tutorial as that version of PHP is easily installed. If you&#8217;re on RHEL or Centos then I&#8217;d suggest taking a look at the IUS Community Project for getting a recent PHP version.</p>
<h2>Step 1: Setup</h2>
<p>Download Fat-Free Framework (F3) from the website.<br />
F3 works just as happily in the site root as from a subdirectory &#8211; I&#8217;ll assume you&#8217;re using a subdirectory here as it means you won&#8217;t have to set up a seperate site to do this tutorial.<br />
Create a folder called <code>blog</code> and unzip the contents of the F3 download into that. It should look like this:<br />
Move up one level in the directory heirarchy and set the permissions:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chgrp</span> <span style="color: #660033;">-R</span> www-data blog
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #660033;">-R</span> <span style="color: #000000;">775</span> blog</pre></div></div>

<p>The folder contents should look like this:<br />
<a href="http://www.willis-owen.co.uk/wp-content/uploads/2011/09/1.png"><img src="http://www.willis-owen.co.uk/wp-content/uploads/2011/09/1.png" alt="Folder Contents" title="Folder Contents" width="389" height="144" class="alignnone size-full wp-image-282" /></a></p>
<p>Notice how much simpler the starting site is compared with Slim + extra packages.</p>
<p>If using Apache, mod_rewrite will need to be running. Edit <code>.htaccess</code> and adjust <code>RewriteBase</code> to add the blog subfolder that you are using, so it looks like <code>RewriteBase /blog</code>.</p>
<p>You can browse this site right away and get the F3 start page.<br />
<a href="http://www.willis-owen.co.uk/wp-content/uploads/2011/09/2.png"><img src="http://www.willis-owen.co.uk/wp-content/uploads/2011/09/2-300x197.png" alt="start page" title="Start Page" width="300" height="197" class="alignnone size-medium wp-image-284" /></a><br />
(Once the site has been visited, F3 will create additional folders cache and temp &#8211; you don&#8217;t need to worry about these).</p>
<h2>Step 2: Bootstrapping</h2>
<p>As everything we need is already part of F3 you don&#8217;t need to do anything here!</p>
<p>You can however tidy up the site to remove the current home page and add a database connection.<br />
Edit index.php<br />
comment out the CACHE option and increase the DEBUG level to help while developing, then remove the only route command. It should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require</span> __DIR__<span style="color: #339933;">.</span><span style="color: #0000ff;">'/lib/base.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//F3::set('CACHE',TRUE);</span>
F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DEBUG'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'UI'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'ui/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
F3<span style="color: #339933;">::</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>To setup a database connection add the following between the set and run commands:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">new</span> DB<span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'mysql:host=localhost;port=3306;dbname=YourDatabaseName'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'YourUsername'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'YourPassword'</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All the User Interface files will go in the ui directory, you can delete <code>welcome.htm</code> and <code>style.css</code> from here as they were just used by the default home page.</p>
<h2>Step 3: Routing</h2>
<p>Similar to Slim you need to tell F3 the route request method (GET, POST, PUT etc), the URI to respond to and how to respond.<br />
Home page route:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET /'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//do something</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The anonymous function will contain the logic to populate the page.<br />
To view a blog article:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET /view/@id'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> F3<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PARAMS[&quot;id&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This tells F3 to expect a URI parameter and assigns it to a PHP variable in the anonymous function.</p>
<p>Now the administration routes:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Admin Home</span>
F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET /admin'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Admin Add</span>
F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET /admin/add'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Admin Edit </span>
F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET /admin/edit/@id'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> F3<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PARAMS[&quot;id&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Admin Add &amp; Edit, Deal with Form Posts</span>
F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'POST /admin/edit/@id'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'edit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'POST /admin/add'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'edit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">function</span> edit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Admin Delete</span>
F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET /admin/delete/@id'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> F3<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PARAMS[&quot;id&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Notice that we&#8217;re using the same function to process Add and Edit form posts so it isn&#8217;t anonymous but the function name is passed as a parameter to the route command.</p>
<h2>Step 4: Models</h2>
<p>The ORMs in Fat-Free Framework do all the hard work for you &#8211; no directories, files or code required here.<br />
Are you beginning to see how much simpler this is compared with Slim?</p>
<p>Here&#8217;s the SQL that will set you up with the 2 tables necessary for this tutorial:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> <span style="color: #ff0000;">`blog`</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_general_ci;
&nbsp;
<span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #ff0000;">`blog`</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`article`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`timestamp`</span> datetime <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`title`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`summary`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`content`</span> text <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`author`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM  <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`article`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`timestamp`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`title`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`summary`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`content`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`author`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2011-07-28 02:03:14'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Hello World!'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Mr White'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2011-07-28 02:03:14'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'More Hello World!'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Mr Green'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`user`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`password`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`user`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`password`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'admin'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'password'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<h2>Step 5: Application Front End</h2>
<p>Like this Slim tutorial we&#8217;re going to keep this simple.<br />
Instantiate an Axon object that interacts with the users table, call the afind method to return a simple array of results, finally the set command is used which will pass the variable between MVC components. F3 calls this a framework variable.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000088;">$article</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Axon<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'article'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$articles</span><span style="color: #339933;">=</span><span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">afind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'articles'</span><span style="color: #339933;">,</span><span style="color: #000088;">$articles</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You could condense the final two lines together like <code>F3::set('articles',$article->afind());</code> but I&#8217;m keeping this verbose to aid readability and help you figure out how it is all working.</p>
<p>To use templating you need a base layout file in the ui folder called <code>layout.html</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">@</span>html_title<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>meta charset<span style="color: #339933;">=</span><span style="color: #0000ff;">'utf8'</span> <span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>F3<span style="color: #339933;">:</span><span style="color: #b1b100;">include</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{{@content}}&quot;</span><span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>

<p>The F3 template engine uses {{@name}} to write out the value of a framework variable.<br />
The F3:include directive will embed the contents of a file at the position where the directive is stated &#8211; in the example above we&#8217;re using a framework variable as the URL so that the content is dynamic.</p>
<p>Now lets create the first of those content files with the view for the homepage, called blog_home.html</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Blog Titles<span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>F3<span style="color: #339933;">:</span>repeat group<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{{@list}}&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{{@item}}&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;view/{{@item['id']}}&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span>item<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> by <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">@</span>item<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'author'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">@</span>item<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'summary'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>F3<span style="color: #339933;">:</span>repeat<span style="color: #339933;">&gt;</span></pre></div></div>

<p>The F3:repeat directive will loop through an array, setting item to the current array element. Within the loop item contains the array of data retrived from the database table and this is accessed using the column name as the array key.</p>
<p>Now that the view is in place we can complete the code in index.php to display it. Set the framework variable to tell the template which view to include, then tell F3 to serve the template.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'blog_home.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> Template<span style="color: #339933;">::</span><span style="color: #004000;">serve</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Serving the template also converts it to PHP code the first time it&#8217;s used and this optimised version is used thereafter which is great for performance.<br />
The full code for this is:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET /'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'html_title'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Home Page'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Axon<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'article'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'list'</span><span style="color: #339933;">,</span><span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">afind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'blog_home.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> Template<span style="color: #339933;">::</span><span style="color: #004000;">serve</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now for the detail view, in index.php we need to get an Axon object, search for the id then send the data to the view/template. In this case we could have assigned each value individually e.g. <code>F3::set('title',$article->title);</code> but it is quicker to put all the values in the POST framework variable with the copyTo command.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET /view/@id'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> F3<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PARAMS[&quot;id&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//create Axon object and search for id</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Axon<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'article'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//set framework variables</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'html_title'</span><span style="color: #339933;">,</span><span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">copyTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//serve up the view</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'blog_detail.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> Template<span style="color: #339933;">::</span><span style="color: #004000;">serve</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And the view file itself called <code>blog_detail.html</code> accesses the individual data items from the POST framework variable:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">@</span>POST<span style="color: #339933;">.</span>title<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Published<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">@</span>POST<span style="color: #339933;">.</span>timestamp<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span> by <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">@</span>POST<span style="color: #339933;">.</span>author<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">@</span>POST<span style="color: #339933;">.</span>content<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">'../'</span><span style="color: #339933;">&gt;</span>Back to Homepage<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span></pre></div></div>

<h2>Step 6: Application Back End</h2>
<p>The admin home page just needs to list the blog articles and provide links, the code is similar to that for the homepage.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET /admin'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'html_title'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'My Blog Administration'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Axon<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'article'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$list</span><span style="color: #339933;">=</span><span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">afind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'list'</span><span style="color: #339933;">,</span><span style="color: #000088;">$list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'admin_home.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> Template<span style="color: #339933;">::</span><span style="color: #004000;">serve</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The view called <code>admin_home.html</code> and contains a table of the results. It looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>My Blog Administration<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">'admin/edit'</span><span style="color: #339933;">&gt;</span>Add Article<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>thead<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Title<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Date<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span>Author<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>th colspan<span style="color: #339933;">=</span><span style="color: #0000ff;">'2'</span><span style="color: #339933;">&gt;</span>Actions<span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>thead<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>tbody<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>F3<span style="color: #339933;">:</span>repeat group<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{{@list}}&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{{@item}}&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">@</span>item<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">@</span>item<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">@</span>item<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'author'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;admin/edit/{{@item['id']}}&quot;</span><span style="color: #339933;">&gt;</span>Edit<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;admin/delete/{{@item['id']}}&quot;</span><span style="color: #339933;">&gt;</span>Delete<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>td<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>F3<span style="color: #339933;">:</span>repeat<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>tbody<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span></pre></div></div>

<p>The output of this look like:<br />
<a href="http://www.willis-owen.co.uk/wp-content/uploads/2011/09/3.png"><img src="http://www.willis-owen.co.uk/wp-content/uploads/2011/09/3-300x121.png" alt="Admin Home Page" title="Admin Home Page" width="300" height="121" class="alignnone size-medium wp-image-287" /></a><br />
Now a form to add and edit articles, called <code>admin_edit.html</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>Edit<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>form name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;blog&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{{ @BASE }}{{ @PARAMS.0 }}&quot;</span> <span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>F3<span style="color: #339933;">:</span>check <span style="color: #b1b100;">if</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{{ @message }}&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;</span>span <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fail&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span> <span style="color: #339933;">@</span>message <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>F3<span style="color: #339933;">:</span>check<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">&gt;</span>Title<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;title&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;title&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{{ htmlspecialchars(@POST.title) }}&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;60&quot;</span><span style="color: #339933;">/&gt;&lt;</span>br <span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'author'</span><span style="color: #339933;">&gt;</span>Author<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;author&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;author&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{{ htmlspecialchars(@POST.author) }}&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;60&quot;</span><span style="color: #339933;">/&gt;&lt;</span>br <span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'summary'</span><span style="color: #339933;">&gt;</span>Summary<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>textarea name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;summary&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;summary&quot;</span> cols<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;60&quot;</span> rows<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span>POST<span style="color: #339933;">.</span>summary<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>textarea<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">&gt;</span>Content<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>textarea name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;content&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;content&quot;</span> cols<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;60&quot;</span> rows<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span>POST<span style="color: #339933;">.</span>content<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>textarea<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Submit&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></div></div>

<p>I&#8217;ve kept this basic, apologies for the lack of styling but that&#8217;s not what this tutorial is about.<br />
Note that there&#8217;s an area to display validation messages.</p>
<p>Now for the logic with the routes, add &#038; edit both use the same view remember.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET /admin/add'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'html_title'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'My Blog Create'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'admin_edit.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> Template<span style="color: #339933;">::</span><span style="color: #004000;">serve</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
F3<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GET /admin/edit/@id'</span><span style="color: #339933;">,</span>
	<span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'html_title'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'My Blog Edit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> F3<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PARAMS[&quot;id&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Axon<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'article'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">copyTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'admin_edit.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> Template<span style="color: #339933;">::</span><span style="color: #004000;">serve</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'layout.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now we assigned a function for the POST routes and here&#8217;s the content:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">function</span> edit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Reset previous error message, if any</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> F3<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PARAMS[&quot;id&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> Axon<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'article'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//load in the article, set new values then save</span>
		<span style="color: #666666; font-style: italic;">//if we don't load it first Axon will do an insert instead of update when we use save command</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id='<span style="color: #006699; font-weight: bold;">$id</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//overwrite with values just submitted</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">copyFrom</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//create a timestamp in MySQL format</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timestamp</span><span style="color: #339933;">=</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</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;">// Return to admin home page, new blog entry should now be there</span>
		F3<span style="color: #339933;">::</span><span style="color: #004000;">reroute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/admin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There&#8217;s a lot less coding required here than with Slim + extras.</p>
<h2>Step 7: Using Middleware</h2>
<p>This isn&#8217;t relevant to using the Fat-Free Framework.<br />
Authentication using a database is built in so I&#8217;ll step through how to use it.<br />
The following lines are added to the admin homepage route</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//tell F3 the database table and fields for user id and password</span>
F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AUTH'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'table'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'user'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'pw'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'password'</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;">//carry out authentication</span>
<span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> Auth<span style="color: #339933;">::</span><span style="color: #004000;">basic</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sql'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//if successful, set a framework variable</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$auth</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//set the session so user stays logged in</span>
  F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SESSION.user'</span><span style="color: #339933;">,</span><span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//display the admin view</span>
  F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'admin_home.html'</span><span style="color: #009900;">&#41;</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: #666666; font-style: italic;">//login unsuccessful so display message</span>
	F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'security.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><code>security.html</code> looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;p&gt;You must supply valid login details.&lt;/p&gt;</pre></div></div>

<p>In the add &#038; edit routes, add this line before <code>Template::serve</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>F3<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SESSION.user'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> F3<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'security.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That&#8217;s it (if you didn&#8217;t spot it in the SQL, my example uses admin &#038; password for the login credentials).<br />
I did get a bug with version 2.0.5, auth.php line 230 if you get this then that line should read <code>self::HTTP_WebAuth</code> instead of <code>HTTP_WebAuth</code> on its own.</p>
<p>You could instead choose to redirect back to the homepage with:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>F3<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SESSION.user'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> F3<span style="color: #339933;">::</span><span style="color: #004000;">reroute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This would also work in the start of delete and POST routes and may make the application more secure, <code>security.html</code> wouldn&#8217;t be required.</p>
<h2>Step 8: Summary</h2>
<p>So here is another example of how to quickly get a prototype running using a PHP micro framework.<br />
Is it any faster than with Slim? I&#8217;d like to think that it is, there&#8217;s less code, less complexity and you aren&#8217;t dependent on other packages which may end up changing in some way or not being maintained.<br />
I&#8217;ve used F3 to create a times tables application for my son and found it was fun to build and made me more productive &#8211; I&#8217;ve now put it online at <a href='http://www.times-tables.info'>www.times-tables.info</a> for anyone to use.</p>
<p>I should add that Licensing of Fat-Free Framework means for academic and personal use it uses the GNU General Public License and is free, however for business or commercial gain you need to contact the developers.<br />
Slim is released under the MIT Public License and you are free to use, modify and even sell it.</p>
<p>You can download the files used from here <a href='http://www.willis-owen.co.uk/wp-content/uploads/2011/09/blog.zip'>blog.zip</a>.</p>
<p>I&#8217;ve done another tutorial on creating a near identical blog application to this but using CakePHP: <a href="/2011/10/blog-tutorial-with-cakephp-framework/">Blog Tutorial with CakePHP Framework</a> which may be interesting to compare with differences with using a micro-framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willis-owen.co.uk/2011/09/blog-tutorial-with-fat-free-framework/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP Frameworks</title>
		<link>http://www.willis-owen.co.uk/2011/09/php-frameworks/</link>
		<comments>http://www.willis-owen.co.uk/2011/09/php-frameworks/#comments</comments>
		<pubDate>Fri, 30 Sep 2011 08:58:08 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.willis-owen.co.uk/?p=236</guid>
		<description><![CDATA[Here&#8217;s my very basic research into current PHP Frameworks &#8211; the target was to get this done in an hour. Firstly if they have a professional design I&#8217;m assuming there&#8217;s at least a bit of funding behind the project. Secondly &#8230; <a href="http://www.willis-owen.co.uk/2011/09/php-frameworks/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s my very basic research into current PHP Frameworks &#8211; the target was to get this done in an hour.</p>
<p>Firstly if they have a professional design I&#8217;m assuming there&#8217;s at least a bit of funding behind the project.<br />
Secondly I do a simple search for AJAX in their documentation. If they don&#8217;t mention how to use the framework with AJAX then I&#8217;m not interested.</p>
<table>
<tbody>
<tr>
<th>Name</th>
<th>PHP Version</th>
<th>Professional Design</th>
<th>Search AJAX in Help</th>
<th>Worth further investigation</th>
</tr>
<tr>
<td>Kohana<br />
(CodeIgniter clone)</td>
<td>5</td>
<td>Yes</td>
<td>No</td>
<td>No &#8211; more CodeIgniter jobs about</td>
</tr>
<tr>
<td>Yii</td>
<td>5</td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>Lithium<br />
(CakePHP clone)</td>
<td>5.3</td>
<td>Yes</td>
<td>No</td>
<td>No &#8211; more CakePHP jobs about</td>
</tr>
<tr>
<td>Akelos</td>
<td>4</td>
<td>Yes</td>
<td>No</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>PHP on Trax<br />
(based on Ruby on Rails)</td>
<td>?</td>
<td>Not really</td>
<td>No</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Fuel</td>
<td>5.3</td>
<td>Yes</td>
<td>No</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Agile Toolkit</td>
<td>5.3</td>
<td>Yes</td>
<td>Yes</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CodeIgniter</td>
<td>5.1.6</td>
<td>Yes</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>CakePHP (V2)</td>
<td>5.2.9</td>
<td>Yes</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Solar</td>
<td>5.2</td>
<td>Yes</td>
<td>No</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Rain</td>
<td>5</td>
<td>Yes</td>
<td>A little</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Recess</td>
<td>5.2.4</td>
<td>Yes</td>
<td>No</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<p>PHP Micro frameworks:</p>
<ul>
<li>DooPHP</li>
<li>Fat-Free Framework</li>
<li>Limondae</li>
<li>Slim</li>
</ul>
<p>I looked in detail at Agile toolkit but ultimately found their documentation too limited.<br />
Recess looked good because of its focus on RESTful API which is built in &#8211; however I&#8217;m also keen on built in validation which this doesn&#8217;t have yet.</p>
<p>In summary for me it boils down to 2 factors, is it popular framework where jobs are advertised as requiring that knowledge and/or is it going to give a big advantage in developing applications rapidly. I&#8217;m going to focus my efforts on CakePHP (I&#8217;ve already built an app using 1.2.6 and version 2 will be launched very soon), CodeIgniter, Yii and also <a href='/2011/09/blog-tutorial-with-fat-free-framework/' title='blog tutorial with Fat-Free Framework'>Fat-Free Framework</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willis-owen.co.uk/2011/09/php-frameworks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Top 100 Best Companies To Work For &#8211; without paywall</title>
		<link>http://www.willis-owen.co.uk/2011/09/top-100-best-companies-to-work-for-without-paywall/</link>
		<comments>http://www.willis-owen.co.uk/2011/09/top-100-best-companies-to-work-for-without-paywall/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 08:43:02 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.willis-owen.co.uk/?p=260</guid>
		<description><![CDATA[I was looking at a job advert the other day and the company was advertised as being a &#8220;Times Top 100 Best Companies to work for organisation&#8221;. I tried searching to see what this actually meant but this which is &#8230; <a href="http://www.willis-owen.co.uk/2011/09/top-100-best-companies-to-work-for-without-paywall/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was looking at a job advert the other day and the company was advertised as being a &#8220;Times Top 100 Best Companies to work for organisation&#8221;. I tried searching to see what this actually meant but this which is published by the Sunday Times is behind their paywall &#8211; meaning that you need to subscribe to see the content.</p>
<p>I don&#8217;t give up easily on tasks like this and after a little more searching I found that the list was publicly available at the following: URL: <a title="best companies to work for list" href="http://www.bestcompanies.co.uk//list_intro.aspx">http://www.bestcompanies.co.uk/list_intro.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.willis-owen.co.uk/2011/09/top-100-best-companies-to-work-for-without-paywall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Moving on from Connells</title>
		<link>http://www.willis-owen.co.uk/2011/08/future-opportunities/</link>
		<comments>http://www.willis-owen.co.uk/2011/08/future-opportunities/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 13:12:05 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.willis-owen.co.uk/?p=241</guid>
		<description><![CDATA[I have resigned from my position as Leading Web Developer for Connells Estate Agents today for a variety of reasons. More importantly the future is now full of opportunities. I&#8217;m keeping on open mind on any permanent or contract job &#8230; <a href="http://www.willis-owen.co.uk/2011/08/future-opportunities/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I have resigned from my position as Leading Web Developer for Connells Estate Agents today for a variety of reasons. More importantly the future is now full of opportunities. I&#8217;m keeping on open mind on any permanent or contract job offers that come my way but I&#8217;m not going to be idle whilst waiting for work.</p>
<p>I have several website ideas that I hope to develop to fruition in the near future. Along with Paul Betteridge who was previously Head of Online Operations for Connells and prior to that Managing Director of Nethouseprices, we&#8217;re planning on starting our own agency &#8216;Equipotent&#8217;. We&#8217;ve got a lot of knowledge on building/running websites in the property sector and hoping to expand that to other areas.</p>
<p>More details soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willis-owen.co.uk/2011/08/future-opportunities/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing dynamic iframe content with JavaScript</title>
		<link>http://www.willis-owen.co.uk/2011/07/writing-dynamic-iframe-content-with-javascript/</link>
		<comments>http://www.willis-owen.co.uk/2011/07/writing-dynamic-iframe-content-with-javascript/#comments</comments>
		<pubDate>Sat, 30 Jul 2011 13:07:40 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.willis-owen.co.uk/?p=226</guid>
		<description><![CDATA[I recently had the need to put a preview of some HTML in a page for a content management system. Simply inserting the HTML into the page wasn&#8217;t any good because it would then use the pages styling and so &#8230; <a href="http://www.willis-owen.co.uk/2011/07/writing-dynamic-iframe-content-with-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently had the need to put a preview of some HTML in a page for a content management system. Simply inserting the HTML into the page wasn&#8217;t any good because it would then use the pages styling and so look nothing like how it would appear once live.</p>
<p>I could have set about making some large changes to the CSS so that the current styling only applied to content in one division, and in the preview area a totally different set of styles applied but that would have been time consuming.</p>
<p>The solution I came up with was to use an iframe tag which essentially is a brand new web browser window &#8211; and put the new content into ther</p>
<p>The first attempt was unsuccessful (code simplified):</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">iframe</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript:void(0);&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'/css/style.css'</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'stylesheet'</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/css'</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>preview content goes here<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">iframe</span>&gt;</span></pre></div></div>

<p>Unfortunately that didn&#8217;t work out. As an iframe is the container for another html document you are meant to provide a location for that document via the src attribute. Content between the iframe tags is ignored if the browser supports the tag or displayed if the browser doesn&#8217;t recognise the iframe tag. The example above gives a &#8216;page not found&#8217; message in a modern browser.</p>
<p>The way that did work in the end was using JavaScript, and it was suprisingly simple (no external libraries required).</p>
<p>write iframe tag (this time empty)<br />
write a hidden form field containing the content &#8211; be sure to escape quotes &#34; -&gt; &amp;#34;<br />
run JavaScript immediately after form field is written, picking up its contents and writing these into the iframe</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'hidden'</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'hidden1'</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span>\<span style="color: #ff0000;">&quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;link href='/css/fckeditor.css' rel='stylesheet' type='text/css' /&gt;&lt;/head&gt;&lt;body&gt;</span></span> content <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/javascript'</span>&gt;</span>mydoc = document.getElementById('content1').contentWindow.document; mydoc.write(document.getElementById('hidden1').value); mydoc.close();<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>This works really well and solved my problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willis-owen.co.uk/2011/07/writing-dynamic-iframe-content-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 28.532 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2011-12-06 17:15:36 -->
<!-- Compression = gzip -->
