<?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>actionshrimp.com &#187; website</title>
	<atom:link href="http://www.actionshrimp.com/tag/website/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.actionshrimp.com</link>
	<description>fun and geekery</description>
	<lastBuildDate>Thu, 12 May 2011 20:12:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>CakePHP Tutorial Part 2: Authentication and Tweaking</title>
		<link>http://www.actionshrimp.com/2009/03/cakephp-tutorial-part-2-authentication/</link>
		<comments>http://www.actionshrimp.com/2009/03/cakephp-tutorial-part-2-authentication/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 19:51:59 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[cake]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.actionshrimp.com/?p=178</guid>
		<description><![CDATA[In the first part of my CakePHP tutorial, I showed you how to use Cake&#8217;s Bake utility to set up the basic back end of an online illustration portfolio. In this part, I&#8217;ll tweak the automatically generated code into useable website and admin section and show you how to use some of Cake&#8217;s features along [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.actionshrimp.com/wordpress/wp-content/uploads/2009/03/cake-logo1.png"><img class="alignleft size-full wp-image-252" title="cake-logo1" src="http://www.actionshrimp.com/wordpress/wp-content/uploads/2009/03/cake-logo1.png" alt="cake-logo1" width="180" height="180" /></a>In the <a href="http://www.actionshrimp.com/2009/03/cakephp-tutorial-part-1-bake-utility/">first part of my CakePHP tutorial</a>, I showed you how to use Cake&#8217;s Bake utility to set up the basic back end of an online illustration portfolio. In this part, I&#8217;ll tweak the automatically generated code into useable website and admin section and show you how to use some of Cake&#8217;s features along the way, including the Authentication component.<br />
<span id="more-178"></span></p>
<h3>Joining the sections together</h3>
<p>If you followed the first part of the tutorial, we currently have a few sections that we&#8217;ve generated models, controllers and views for. By navigating to your_app/controllername/action in your browser, you can access the different functions of the application. But unless your users know this in advance, they have no way of accessing them.</p>
<p>The easiest way of implementing navigation common to all pages is to use Cake&#8217;s built in templating system, the page &#8220;layout&#8221;. Cake uses its built-in layout if it finds no user defined layout default available. To add one, create the file your_app_dir/app/views/layouts/default.ctp, and put in your basic page layout. Mine looked like this:</p>
<pre class="brush: php">
&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;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;&lt;?php echo $title_for_layout ?&gt;&lt;/title&gt;
&lt;link href=&quot;favicon.ico&quot; type=&quot;image/x-icon&quot; rel=&quot;shortcut icon&quot; /&gt;
&lt;?php echo $html-&gt;css(&#039;default&#039;) ?&gt;
&lt;?php echo $scripts_for_layout ?&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;container&quot;&gt;
&lt;div id=&quot;header&quot;&gt;
&lt;h1&gt;King-Jon Illustration&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;navigation&quot;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;?php echo $html-&gt;link(&#039;News&#039;, array(&#039;controller&#039;=&gt;&#039;News&#039;))?&gt;&lt;/li&gt;
&lt;li&gt;&lt;?php echo $html-&gt;link(&#039;Works&#039;, array(&#039;controller&#039;=&gt;&#039;Works&#039;))?&gt;&lt;/li&gt;
&lt;li&gt;&lt;?php echo $html-&gt;link(&#039;Sketches&#039;, array(&#039;controller&#039;=&gt;&#039;Sketches&#039;))?&gt;&lt;/li&gt;
&lt;li&gt;&lt;?php echo $html-&gt;link(&#039;Friends&#039;, array(&#039;controller&#039;=&gt;&#039;Friends&#039;))?&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&quot;content&quot;&gt;
&lt;?php echo $content_for_layout ?&gt;
&lt;/div&gt;
&lt;div id=&quot;footer&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>You&#8217;ll notice this looks like a pretty normal basic page skeleton, with a few extra bits of PHP inserted. The $title_for_layout, $scripts_for_layout and $content_for_layout variables are filled in by Cake automatically depending on what the current page request is and the title and content are fairly self-explanitory.  The scripts variable lets you specify scripts on a per-page basis but still include them in the &lt;head&gt; section.  The $html-&gt;xxx code snippets are part of the <a href="http://book.cakephp.org/view/206/Inserting-Well-Formatted-elements">Cake HtmlHelper</a>. This allows you to insert well formatted elements easily &#8211; the $html-&gt;css(&#8216;default&#8217;) includes the stylesheet &#8220;css/default.css&#8221; (this is stored in the your_app_dir/app/webroot/css directory), and the $html-&gt;link lines let you point to different parts of your app easily. You could just add normal links, such as &lt;a href=&#8221;/news/&#8221;&gt;, but pointing to the controller allows a bit of flexibility if you want to change anything further down the line.</p>
<h3>Adding a splash page</h3>
<p>As it was an art portfolio website, it was decided a splash page would be useful to have, to give a good first impact when entering the site and the ability to show off a bit of artwork right away. Cake uses its built-in pages controller to handle static pages, and the different pages are stored in the folder your_app_dir/app/views/pages. To view a page, you navigate to the URL your_app/pages/pagename. Here, the pages controller doesn&#8217;t take an action, so the additional directory level on the URL is an argument passed to the controller, in this case the name of the page to display.</p>
<p>Cake also automatically serves up a default page, called &#8220;home&#8221;, when a user requests the root of your_app. The name of this page is set by Cake&#8217;s router, and if we look in your_app_dir/app/config/routes.php, we see at the bottom, the lines:</p>
<pre class="brush: php">
/**
* Here, we are connecting &#039;/&#039; (base path) to controller called &#039;Pages&#039;,
* its action called &#039;display&#039;, and we pass a param to select the view file
* to use (in this case, /app/views/pages/home.ctp)...
*/
Router::connect(&#039;/&#039;, array(&#039;controller&#039; =&gt; &#039;pages&#039;, &#039;action&#039; =&gt; &#039;display&#039;, &#039;home&#039;));
/**
* ...and connect the rest of &#039;Pages&#039; controller&#039;s urls.
*/
Router::connect(&#039;/pages/*&#039;, array(&#039;controller&#039; =&gt; &#039;pages&#039;, &#039;action&#039; =&gt; &#039;display&#039;));
</pre>
<p>The comments tell us what&#8217;s going on &#8211; this is what maps the pages to the locations I have talked about. You can add your own mappings here too if you like.  For our splash screen however, we can just make use of the already defined &#8220;home&#8221; page, so edit this to make it look how you want, you&#8217;ll find it in your_app_dir/app/views/pages/home.ctp. Mine looked like this:</p>
<pre class="brush: php">
&lt;?php $this-&gt;layout = &quot;splash&quot; ?&gt;
&lt;div id=&quot;splash&quot;&gt;
&lt;?php echo $html-&gt;image(&quot;splash.png&quot;,
array(&quot;alt&quot; =&gt; &quot;King Jon Illustration&quot;,
&quot;url&quot; =&gt; array(&quot;controller&quot; =&gt; &quot;news&quot;)));?&gt;
&lt;/div&gt;
</pre>
<p>A couple of things to note here. The first line $this-&gt;layout = &#8220;splash&#8221;, tells Cake not to use the &#8220;default&#8221; layout we created earlier, but to instead use one called &#8220;splash&#8221;.  I sneakily created a splash layout without telling you here, but it is in fact just an exact copy of the default layout with the navigation div removed, to give the page a more &#8220;splashy&#8221; feel.</p>
<p>Next, I used the image HtmlHelper to include an image called &#8220;splash.png&#8221; &#8211; this is located in my_app_dir/app/webroot/img, the location the image helper looks in. The second argument is an array of additional attributes, &#8220;alt&#8221; being the alt text, and &#8220;url&#8221; puts an &lt;a&gt; element around the image, creating a link to the specified address.</p>
<h3>Admin section authentication</h3>
<p><a href="http://www.actionshrimp.com/wordpress/wp-content/uploads/2009/03/authentic-screen.png"><img class="alignleft size-medium wp-image-251" title="auth-screen" src="http://www.actionshrimp.com/wordpress/wp-content/uploads/2009/03/authentic-screen-300x187.png" alt="auth-screen" width="300" height="187" /></a>At the minute, all our sections are wide open for anyone to just stroll in and start adding news, posting works, and generally delete or edit existing pieces of data. This clearly isn&#8217;t ideal, so next on the agenda is sorting this out. We&#8217;ll use the <a href="http://book.cakephp.org/view/172/Authentication">Authentication component</a> for this.</p>
<p>First of all, we want all add/edit capabilities to be kept within the admin section, so the first step is to remove the non-admin versions of these. Go through all five of the your_app_dir/app/controllers/*_controller.php, and delete the functions &#8220;add&#8221; and &#8220;edit&#8221; that were automatically generated. Next, clean up the views associated with these functions &#8211; delete the two files your_app_dir/app/views/*/add.ctp and edit.ctp in all five of the view directories. This means now we can only add and edit our data by navigating to your_app/<strong>admin</strong>/controller/add etc.</p>
<p>We&#8217;re going to want to use authentication for all our controllers, as all of them have an admin section that we need to login for. Instead of telling each controller to use this component individually, we can tell the whole controller superclass that we want to use it, to save repetition. Open the file your_app_dir/app/app_controller.php and add the line in the class definition like so:</p>
<pre class="brush: php">

class AppController extends Controller {
var $components = array(&#039;Auth&#039;);
}
</pre>
<p>Now if you try and visit an admin page in your_app, you&#8217;ll be greeted with an error messages telling you there&#8217;s no login action in the Users controller, which is where the Auth component looks by default, so we&#8217;d better add this in. Edit the file your_app_dir/app/controllers/users_controller.php, and right at the bottom of the class definition, add these two functions:</p>
<pre class="brush: php">

function admin_login() {
}
function admin_logout() {
$this-&gt;Redirect($this-&gt;Auth-&gt;logout());
}
</pre>
<p>Now try and navigate to an admin section again. You&#8217;re told that there&#8217;s a missing view this time! Better do what Cake says and add one in. Create the new file your_app_dir/app/views/users/admin_login.ctp, and fill it with the following skeleton login form:</p>
<pre class="brush: php">

&lt;?php
if  ($session-&gt;check(&#039;Message.auth&#039;)) $session-&gt;flash(&#039;auth&#039;);
echo $form-&gt;create(&#039;User&#039;, array(&#039;action&#039; =&gt; &#039;admin_login&#039;));
echo $form-&gt;input(&#039;username&#039;);
echo $form-&gt;input(&#039;password&#039;);
echo $form-&gt;end(&#039;Login&#039;);
?&gt;
</pre>
<p>Now there are two problems: we&#8217;re greeted with a login for all the admin sections &#8211; a slight problem as we don&#8217;t have credentials&#8230; we&#8217;ve locked ourselves out of the whole website! There are also some error messages when we try and view the non-admin sections of the site, which seem to want us to login but can&#8217;t find a login() action (remember we only created admin_login, and didnt bother with login). Fortunately, we can get around these problems by adding exceptions to the authentication. Open up the users controller, your_app_dir/app/controllers/users_controller.php, and then add in the following <a href="http://book.cakephp.org/view/60/Callbacks">controller callback function</a> (I added it in near the top):</p>
<pre class="brush: php">

function beforeFilter() {
$this-&gt;Auth-&gt;allow(&#039;admin_add&#039;, &#039;admin_index&#039;);
}
</pre>
<p>This gives access to the admin add and index (which lists all users) actions of the users controller. So now, navigate to your_app/admin/users/add, and create a login for yourself! You&#8217;ll probably want to remove the allow line once you&#8217;ve added yourself to prevent anyone else creating a login for themselves when you&#8217;re not looking.</p>
<p>We&#8217;re still however stuck with the problem that none of the non-admin sections work, so we need to add an Auth exception to all non-admin sections. The best way to do this is by adding the following into the your_app_dir/app/app_controller.php AppController class definition, underneath where we told it to load the Auth component earlier:</p>
<pre class="brush: php">

function beforeFilter(){
$admin = Configure::read(&#039;Routing.admin&#039;);
if (isset($this-&gt;params[$admin]) and $this-&gt;params[$admin]){
//$this-&gt;layout = &#039;admin&#039;;
}
else {
$this-&gt;Auth-&gt;allow();
}
}
</pre>
<p>Now we should be able to navigate the site without any problems. The commented out line above allows the future addition of an admin.ctp layout file that will automatically be loaded when viewing admin sections &#8211; I haven&#8217;t done this yet though.</p>
<p>The next phase of the project is to go through and edit all the views, layouts &amp; CSS and get them looking how you want, and this is largely an exercise in usual web development &#8211; you can take a look at the default views to see how to include data provided by Cake  fairly easily. If I come across anything else I think might be of help while I&#8217;m building the rest of the site, I&#8217;ll make small posts for those too. Hopefully you&#8217;ve been able to follow this introduction to Cake so far, but if not drop me a line in the comments below and I&#8217;ll try and give you a hand where I can. Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.actionshrimp.com/2009/03/cakephp-tutorial-part-2-authentication/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>CakePHP Tutorial Part 1: Bake Utility</title>
		<link>http://www.actionshrimp.com/2009/03/cakephp-tutorial-part-1-bake-utility/</link>
		<comments>http://www.actionshrimp.com/2009/03/cakephp-tutorial-part-1-bake-utility/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 17:42:35 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[bake]]></category>
		<category><![CDATA[cake]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.actionshrimp.com/?p=129</guid>
		<description><![CDATA[A friend of mine recently asked me to help him build an online portfolio for his illustration pieces, and I decided to use CakePHP 1.2 to get the back end up and running quickly. I thought someone might find a tutorial taking a simple website through from start to finish useful, so this tutorial series [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-130" title="cake-logo" src="http://www.actionshrimp.com/wordpress/wp-content/uploads/2009/03/cake-logo.png" alt="cake-logo" width="180" height="180" />A friend of mine recently asked me to help him build an online portfolio for his illustration pieces, and I decided to use <a href="http://cakephp.org/" target="_blank">CakePHP 1.2</a> to get the back end up and running quickly. I thought someone might find a tutorial taking a simple website through from start to finish useful, so this tutorial series will do just that. This part of the series takes you through the first few steps I took to get the basic code together.  You&#8217;ll need a web server and database server such as MySQL, and some basic PHP and webserver management knowledge. We&#8217;ll be using Cake&#8217;s &#8220;bake&#8221; utility to create the basic code as well, so you&#8217;ll need console access to your webserver, and PHP&#8217;s command line utility (in Ubuntu/PHP5 you can just install the package php5-cli).</p>
<p><span id="more-129"></span></p>
<h3>Downloading and setting up CakePHP</h3>
<p>Create a new directory on your webserver where your cake website will reside (or just use the web root if this is going to be the main webpage).  Next, we&#8217;ll need to actually download CakePHP.  If you have shell access to your webserver, you can use SVN to get the latest stable release automatically: navigate to the directory and use the command (note the &#8220;.&#8221; at the end of the command)</p>
<pre>$ svn checkout https://svn.cakephp.org/repo/trunk/cake/1.2.x.x/ .</pre>
<p>to do so.  If you don&#8217;t know what any of this means, just download the latest stable release from the CakePHP website and extract the files into the directory you created &#8211; make sure the various .htaccess files don&#8217;t go astray in the process.</p>
<p>Some standard setup is also required: we need to tell cake how to connect to our database server, to do this follow the steps on <a href="http://book.cakephp.org/view/331/Cake-Database-Configuration">this page</a>. There&#8217;s also a couple of optional (but recommended) configuration steps <a href="http://book.cakephp.org/view/332/Optional-Configuration">here</a>. After this, you should be able to browse to the directory in a web browser and see the default page &#8211; there shouldn&#8217;t be any errors or warnings if all configuration has been completed correctly (N.B. if the default page looks a bit plain &#8211; no colours, CSS styles etc., then you probably have a mod_rewrite issue, check out <a href="http://book.cakephp.org/view/333/A-Note-on-mod_rewrite">this page</a>).</p>
<p>Now we can begin work on the site.</p>
<h3>Bake a cake</h3>
<p>Cake&#8217;s bake utility is a handy way of generating the basic code structure for a web application, saving a lot of the tedious grunt work that often comes with setting up a new project. The general idea is often seen in frameworks that take advantage of the MVC (model-view-controller) programming pattern (such as Ruby on Rails, and others). It involves creating a database structure that describes the various data entites &#8211; or &#8220;models&#8221; &#8211; of our application, then running a script that reads this structure and generates the framework code for the models. It can then generate standard interfaces (or &#8220;views&#8221;) for displaying the data, and &#8220;controllers&#8221; which tie our interfaces and models together, allowing data to be input and manipulated. Of course, these standard pieces of generated code probably won&#8217;t be exactly what we want, but they can provide a great starting point to work from and then tweak.</p>
<p>First of all, we&#8217;ll create our database structure. I&#8217;m using an empty database for this project, although if you&#8217;re using a database with tables from other projects in, you can set Cake to work with a table prefix fairly easily. Let&#8217;s get to work building the tables.</p>
<p>The first model will be &#8220;News&#8221;, containing data for a news section where the owner can post updates and information onto the site.  To represent this, I created a table &#8220;news&#8221;, with four fields:</p>
<ul>
<li><strong>id</strong> &#8211; type: int &#8211; extra: auto_increment &#8211; primary key <em>(A unique identifier for each record)</em></li>
<li><strong>date</strong> &#8211; type: date</li>
<li><strong>title</strong> &#8211; type: text</li>
<li><strong>body</strong> &#8211; type: text</li>
</ul>
<p>The next model is for representing finished works placed onto the site &#8211; the main part of the portfolio. Create a table called &#8220;works&#8221; with the four fields:</p>
<ul>
<li><strong>id</strong> &#8211; type: int &#8211; extra: auto_increment &#8211; primary key</li>
<li><strong>date</strong> &#8211; type: date</li>
<li><strong>title</strong> &#8211; type: text</li>
<li><strong>desc</strong> &#8211; type: text</li>
</ul>
<p>Next up, a sketches model. This is pretty much the same as &#8220;works&#8221; but will contain sketches as opposed to finished pieces. Create a table called &#8220;sketches&#8221; with the same four fields as above.</p>
<p><a href="http://www.actionshrimp.com/wordpress/wp-content/uploads/2009/03/screenshot-1.png"><img class="alignright size-medium wp-image-153" title="database-screenshot" src="http://www.actionshrimp.com/wordpress/wp-content/uploads/2009/03/screenshot-1-300x187.png" alt="database-screenshot" width="300" height="187" /></a>A friends model for a page that will contain links to websites belonging to friends of the owner, along with a thumbnail and description. Create the table &#8220;friends&#8221;, with the fields:</p>
<ul>
<li><strong>id</strong> &#8211; type: int &#8211; extra: auto_increment &#8211; primary key</li>
<li><strong>name</strong> &#8211; type: text</li>
<li><strong>url</strong> &#8211; type:  text</li>
<li><strong>desc</strong> &#8211; type:  text</li>
</ul>
<p>There is one last model we&#8217;ll need: &#8220;users&#8221;. By creating a table with a username/password, we can let the owner login to manage the site, add new works, sketches and friends. Multiple users will mean we can also have a login to test out the site. So create the &#8220;users&#8221; table, with fields:</p>
<ul>
<li><strong>id</strong> &#8211; type: int &#8211; extra: auto_increment &#8211; primary key</li>
<li><strong>username</strong> &#8211; type: varchar(20)</li>
<li><strong>password</strong> &#8211; type:  varchar(50)</li>
<li><strong>displayname</strong> &#8211; type:  varchar(20) (Name shown when making posts etc &#8211; can be different from login name)</li>
</ul>
<p>Now all our tables are created, we can get to baking. In a console, navigate to yourdir/cake/console, and then run the command</p>
<pre>$ ./cake bake</pre>
<p>and you&#8217;ll be presented with:</p>
<pre><a href="http://www.actionshrimp.com/wordpress/wp-content/uploads/2009/03/screenshot-2.png"><img class="alignright size-medium wp-image-154" title="console-screenshot" src="http://www.actionshrimp.com/wordpress/wp-content/uploads/2009/03/screenshot-2-300x187.png" alt="console-screenshot" width="300" height="187" /></a>Welcome to CakePHP v1.2.2.8120 Console
---------------------------------------------------------------
App : app
Path: /home/dave/public_html/kingjon/app
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[Q]uit
What would you like to Bake? (D/M/V/C/P/Q)
&gt;</pre>
<p>at this point, if you haven&#8217;t setup your database configuration yet, you can create it with the &#8220;[D]atabase Configuration&#8221; option. We&#8217;ll be choosing the &#8220;[M]odel&#8221; option however:</p>
<pre>&gt; m
---------------------------------------------------------------
Bake Model
Path: /home/dave/public_html/kingjon/app/models/
---------------------------------------------------------------
Possible Models based on your current database:
1. Friend
2. News
3. Sketch
4. User
5. Work
Enter a number from the list above, type in the name of another model, or 'q' to exit
[q] &gt;</pre>
<p>notice our database tables have been read, and cake has automatically figured out that an entry in &#8220;works&#8221; will be a &#8220;Work&#8221;,  &#8220;sketches&#8221; give a &#8220;Sketch&#8221; and so on, but &#8220;news&#8221; is still &#8220;News&#8221;.  We may as well start with the first entry, so enter 1, and hit enter. You&#8217;ll be asked the following:</p>
<pre>[q] &gt; 1
Would you like to supply validation criteria for the fields in your model? (y/n)
[y] &gt;</pre>
<p>Cake has a set of predefined validation criteria to make sure that data entered for a model is correct. We may as well take advantage of these, so hit &#8220;y&#8221; and press return.</p>
<p>Next Cake goes through each of the fields in our table, and asks what validation criteria we want for each. <strong>id</strong> is first, and I chose &#8220;blank&#8221; (number 3 in my case, athough I guess this number may change depending on which version of cake you&#8217;re using). This ensures that no data is entered for the id, which is what we want as the field is auto generated by mySQL&#8217;s auto_increment. For <strong>name</strong> I chose &#8220;Do not do any validation on this field&#8221; as the name can contain any text. For <strong>url</strong> I chose the rather fitting &#8220;url&#8221; (number 27) validation type. Finally, for <strong>desc</strong> I chose &#8220;Do not do any validation on this field&#8221; as well.</p>
<p>A final couple of questions now pop up: model associations &#8211; for this I just hit &#8220;n&#8221; as our model is simple and has no relational properties &#8211; and a confirmation to check your settings are ok:</p>
<pre>---------------------------------------------------------------
The following Model will be created:
---------------------------------------------------------------
Name:       Friend
Validation: Array
(
    [id] =&gt; blank
    [url] =&gt; url
)

Associations:
---------------------------------------------------------------
Look okay? (y/n)
[y] &gt;</pre>
<p>Confirm this and there&#8217;s a final question about SimpleTest and unit test files. This is to do with Cake&#8217;s testing framework which I personally haven&#8217;t tried, and as this is just a simple project I won&#8217;t other using it, so just hit &#8220;n&#8221; here.  Congratulations, you&#8217;ve successfully baked your first model!</p>
<p>Continue the process now, and bake the other models with appropriate validation criteria (I used &#8220;blank&#8221; for all the IDs, &#8220;date&#8221; for the date fields, and no validation for all the title/description fields &#8211; although for the users table, I chose &#8220;notEmpty&#8221; for the username, password and displayname fields as blank entries here would cause problems).</p>
<p>Next up, time to bake some controllers. I&#8217;ll show you the output from my run through first including the options I chose, and explain to you afterwards what some of them mean and why I chose them:</p>
<pre>What would you like to Bake? (D/M/V/C/P/Q)
&gt; c
---------------------------------------------------------------
Bake Controller
Path: /home/dave/public_html/kingjon/app/controllers/
---------------------------------------------------------------
Possible Controllers based on your current database:
1. Friends
2. News
3. Sketches
4. Users
5. Works
Enter a number from the list above, type in the name of another controller, or 'q' to exit
[q] &gt; 1
---------------------------------------------------------------
Baking FriendsController
---------------------------------------------------------------
Would you like to build your controller interactively? (y/n)
[y] &gt; y
Would you like to use scaffolding? (y/n)
[n] &gt; n
Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)
[n] &gt; y
Would you like to create the methods for admin routing? (y/n)
[n] &gt; y
Would you like this controller to use other helpers besides HtmlHelper and FormHelper? (y/n)
[n] &gt; n
Would you like this controller to use any components? (y/n)
[n] &gt; n
Would you like to use Sessions? (y/n)
[y] &gt; y
You need to enable Configure::write('Routing.admin','admin') in /app/config/core.php to use admin routing.
What would you like the admin route to be?
Example: www.example.com/admin/controller
What would you like the admin route to be?
[admin] &gt; 

---------------------------------------------------------------
The following controller will be created:
---------------------------------------------------------------
Controller Name:  Friends
---------------------------------------------------------------
Look okay? (y/n)
[y] &gt;

Creating file /home/dave/public_html/kingjon/app/controllers/friends_controller.php
Wrote /home/dave/public_html/kingjon/app/controllers/friends_controller.php
SimpleTest is not installed.  Do you want to bake unit test files anyway? (y/n)
[y] &gt; n</pre>
<p>Ok, I started by building the controller for the friends section again. Building interactively gives you access to more options in the setup, so I went for that. <a href="http://book.cakephp.org/view/105/Scaffolding">Scaffolding</a> is basically a flag you set in your controller telling cake that you want basic skeleton app behaviour to be created at runtime. By baking we are given the advantage of pre-generating code that does more or less the same thing, but that we can also edit later, so choosing &#8220;n&#8221; for scaffolding is a wise plan in our case. The next question about basic methods is where our pre-generated skeleton code comes from, so choose &#8220;y&#8221; here. Admin routing creates an admin section for adding/deleting/modifying records. As we don&#8217;t want visitors to the website to be able to do this, but the portfolio owner will be wanting to do this, an admin section is a good idea, so &#8220;y&#8221; to this question as well. We are now asked about <a href="http://book.cakephp.org/view/181/Core-Helpers">helpers</a>. Helpers are libraries built into cake that add functionality and generally make your life easier. At the minute we&#8217;ll just use the default two, so choose &#8220;n&#8221;, although more can be added later easily if we need them. Next up are <a href="http://book.cakephp.org/view/170/Core-Components">components</a> which are similar to helpers, although these are more at a behind-the-scenes level, and helpers are used more for displaying and formatting data on the interface side of things. Again we may use some of these later, but at the minute just hit &#8220;n&#8221; for components. Finally hit &#8220;y&#8221; for sessions, and I agreed to the default name for the admin section, &#8220;admin&#8221;, although you may wish to change this.  Hit &#8220;y&#8221; for the confirmation, and then &#8220;n&#8221; for the testing question again.</p>
<p>Repeat this for the other controllers.</p>
<p>Finally we have to make some views, so here we go again, although this step is a lot quicker. Choose &#8220;[V]iew&#8221; from the main menu, and I&#8217;m starting off with Friends again. The first question it asks is seemly about the scaffolding again:</p>
<pre>Would you like to create some scaffolded views (index, add, view, edit) for this controller?
NOTE: Before doing so, you'll need to create your controller and model classes (including associated models). (y/n)
[n] &gt; y</pre>
<p>Before we wanted nothing to do with scaffolding, but here we hit &#8220;y&#8221;: this generates the views for the basic class methods we agreed to in the controller generation. We&#8217;re then asked about admin routing views, and we press &#8220;y&#8221; again, and the script tells us it&#8217;s generated the files we need.</p>
<p>Repeat this process for the other views, and then we&#8217;re done with baking.</p>
<h3>Let&#8217;s see what we&#8217;ve done so far&#8230;</h3>
<p><a href="http://www.actionshrimp.com/wordpress/wp-content/uploads/2009/03/screenshot.png"><img class="alignleft size-medium wp-image-143" title="defaut-news-screenshot" src="http://www.actionshrimp.com/wordpress/wp-content/uploads/2009/03/screenshot-300x187.png" alt="defaut-news-screenshot" width="300" height="187" /></a>Now what exactly has all that baking achieved? If you go to the index page of your app directory in the web browser, you&#8217;ll just be greeted with the same welcome page. But try navigating to your_app_folder/news or your_app_folder/works and a new page comes up. You can give adding a piece of a news a go with the &#8220;New News&#8221; link on the news page. A form pops up letting you fill in the date, title and post body, and you&#8217;ll notice the entry form lets you fill in the date correctly and so on.  We aren&#8217;t prompted to enter an ID because our validation criteria said we wanted this to be blank as it would be generated automatically. Once the post has been added, an entry comes up on the main page, letting us view the individual post, edit it or delete it.</p>
<p>In fact, if you&#8217;ve been keeping an eye on the URLs, you&#8217;ll have noticed that they take the form your_app_folder/controller/action. The actions were generated automatically by our baking process, and we&#8217;ll take a look at them in a bit more detail later on.</p>
<p>You&#8217;ll notice you can also go to your_app_folder/admin/news to get the same page &#8211; this is our admin routing. At the minute there is no difference between the two sections, but we&#8217;ll edit it so that you wont be able to add/edit/delete thing unless you&#8217;re in the admin section, and logged in correctly.</p>
<p>As you can see, baking has set up all the basic logic for our various sections, we just need to tweak it and glue it together a bit, and I&#8217;ll deal with that in the <a href="http://www.actionshrimp.com/2009/03/cakephp-tutorial-part-2-authentication/">next part of the tutorial</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.actionshrimp.com/2009/03/cakephp-tutorial-part-1-bake-utility/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

