<?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>umop apisdn &#187; cakePHP</title>
	<atom:link href="http://pournaras.zilsen.com/tag/cakephp/feed/" rel="self" type="application/rss+xml" />
	<link>http://pournaras.zilsen.com</link>
	<description>Thoughts, comments, guides and discoveries</description>
	<lastBuildDate>Thu, 17 May 2012 08:26:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Back to school</title>
		<link>http://pournaras.zilsen.com/2009/04/02/back-to-school/</link>
		<comments>http://pournaras.zilsen.com/2009/04/02/back-to-school/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 09:20:49 +0000</pubDate>
		<dc:creator>Takis</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cakePHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://pournaras.zilsen.com/?p=123</guid>
		<description><![CDATA[I&#8217;ve got a couple of messages from [apparently] junior programmers regarding my previous post, so I decided to go back and explain some of the magic that&#8217;s happening every day inside Cake applications. As you know (or should have known) Cake does a lot of magic behind the scenes, as long as we&#8217;re following some [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got a couple of messages from [apparently] junior programmers regarding my previous post, so I decided to go back and explain some of the magic that&#8217;s happening every day inside Cake applications.</p>
<p>As you know (or should have known) Cake does a lot of magic behind the scenes, as long as we&#8217;re following some <a  href="http://book.cakephp.org/view/22/CakePHP-Conventions" title="CakePHP online manual">naming conventions</a>. The developers prefer convention over configuration (via files), which may seem a bit restrictive for some, but, once you get used to it, you will notice that it speeds up things.</p>
<p>If you recall from the previous post, the User Model looks like this:</p>
<pre class="brush: php">
class User extends AppModel {
  var $name = &#039;User&#039;;

}
</pre>
<p>This class extends a class named &#8220;<b>AppModel</b>&#8220;, which in turn extends a class named &#8220;<b>Model</b>&#8220;. Therefore, each method described in the <b>Model</b> class is also available to the <b>AppModel</b> and (our own) the <b>User</b> class in turn.</p>
<p>Why did the developers go so far as to create two classes instead of one? Simply because they wanted to give us more flexibility. We have the option to create a class called AppModel and place that code inside the <b>app</b> directory like this:</p>
<pre class="brush: php">
/**
 *
 * Filename: /app/app_model.php
 *
**/
class AppModel extends Model {

}
</pre>
<p>Now, whenever you create this file, you can add your own custom functions, which will be available to all your models. The same goes for all your controllers, i.e. you can create a file named app_controller.php and place it inside your app directory, where a class named AppController will extend the Controller class.</p>
<p>In case you didn&#8217;t figure it out yourself by now, there is a file named app_model.php which Cake uses if you haven&#8217;t created your own (take a look at /cake/lib/model). Cake automatically includes your own app_model.php if you have one, or its own (empty) version if you don&#8217;t. Talking about some serious magic here!</p>
<p>In general, whenever your application runs, all requests are redirected to the index.php file, which in turn includes all necessary files. Cake examines your app directory and, if it finds files which can override its core files, it includes those files instead.</p>
<p>Finally, if you follow the naming conventions mentioned earlier, you won&#8217;t have to include anything in your files (there are some exceptions to this rule, if you need a certain level of complexity). For instance, if you name your model User, that model will be automatically available in your UsersController controller, simply by typing $this->User.</p>
<p>Catch you all next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://pournaras.zilsen.com/2009/04/02/back-to-school/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to CakePHP authentication</title>
		<link>http://pournaras.zilsen.com/2009/03/29/cakephp-auth/</link>
		<comments>http://pournaras.zilsen.com/2009/03/29/cakephp-auth/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 07:19:22 +0000</pubDate>
		<dc:creator>Takis</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[cakePHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://pournaras.zilsen.com/?p=19</guid>
		<description><![CDATA[This is a tutorial which can be used by those who want to setup a basic user administration and authentication in CakePHP, one of the most popular MVC PHP frameworks around. We will be using the Auth component of Cake and we will create some administration screens. Setting up the database First of all we [...]]]></description>
			<content:encoded><![CDATA[<p>This is a tutorial which can be used by those who want to setup a basic user administration and authentication in <a  title="CakePHP homepage" href="http://cakephp.org">CakePHP</a>, one of the most popular <acronym title="Model-View-Controller">MVC</acronym> PHP frameworks around. We will be using the Auth component of Cake and we will create some administration screens.</p>
<h4>Setting up the database</h4>
<p>First of all we need one table to store our users. Cake&#8217;s Auth module can automate some of the process, as long as we are following some naming conventions. Our table consists of 4 fields: id, username, password and role:</p>
<pre class="brush: sql">
CREATE TABLE users (

  id INT(10) UNSIGNED NOT NULL auto_increment,
  username VARCHAR(20) NOT NULL,
  password VARCHAR(50) NOT NULL,
  role ENUM(&#039;user&#039;,&#039;admin&#039;) NOT NULL DEFAULT &#039;user&#039;,
  PRIMARY KEY  (`id`)

);
</pre>
<p>The &#8220;id&#8221; is the primary key of the table. By using this name, we are helping Cake to take care of possible Model associations without having to modify anything. The pair of fields &#8220;username&#8221; and &#8220;password&#8221; are expected by the Auth component (again, this is configurable). Finally, the &#8220;role&#8221; field will be used in order to distinguish basic users from administrators.</p>
<p>Careful readers will notice that the &#8220;password&#8221; field is rather large. This is because all passwords stored in the database are <a  title="Hash function" href="http://en.wikipedia.org/wiki/Hash_function">hashed</a> and, in the end, the field length will be much longer than the original (depending on the algorithm used).</p>
<h4>Modeling the table</h4>
<p>You can either bake the table into a Model or write the Model on your own. The basic Model looks like this:</p>
<pre class="brush: php">
/**
 *
 * Filename: /app/models/user.php
 *
**/
class User extends AppModel {

  var $name = &#039;User&#039;;

}
</pre>
<p>We are going to add some validation rules:</p>
<ul>
<li>the username must be non-empty and unique</li>
<li>the password must be non-empty</li>
<li>the role can either be &#8216;user&#8217; or &#8216;admin&#8217;</li>
</ul>
<p>Additionally, we would like to have some sort of password confirmation mechanism, which can be used either during the user registration or during the user edit. By implementing those rules, our code becomes:</p>
<pre class="brush: php">
/**
 *
 * Filename: /app/models/user.php
 *
**/
class User extends AppModel {
  var $name = &#039;User&#039;;

  var $validate = array(

    &#039;username&#039; =&gt; array(
      &#039;notEmpty&#039; =&gt; array(
        &#039;rule&#039; =&gt; &#039;notEmpty&#039;,
        &#039;message&#039; =&gt; &#039;The username cannot be empty&#039;
      ),
      &#039;isUnique&#039; =&gt; array(
        &#039;rule&#039; =&gt; &#039;isUnique&#039;,
        &#039;message&#039; =&gt; &#039;The username is already taken.&#039;
      )
    ),

    &#039;password&#039; =&gt; array(
      &#039;notEmpty&#039; =&gt; array(
        &#039;rule&#039; =&gt; &#039;notEmpty&#039;,
        &#039;message&#039; =&gt; &#039;The password cannot be empty&#039;
      ),
      &#039;confirmPassword&#039; =&gt; array(
        &#039;rule&#039; =&gt; array(&#039;confirmPassword&#039;, &#039;password_confirm&#039;),
        &#039;message&#039; =&gt; &#039;Please enter the same password twice&#039;
      )
    ),

    &#039;role&#039; =&gt; array(
      &#039;rule&#039; =&gt; array(&#039;inlist&#039;, array(&#039;user&#039;, &#039;admin&#039;)),
      &#039;message&#039; =&gt; &quot;A user&#039;s role must either be &#039;user&#039; or &#039;admin&#039;&quot;
      )
    );

  function confirmPassword( $original, $confirmationField )  {

    return $this-&gt;data[$this-&gt;name][$confirmationField] === $original[&#039;password&#039;];

  }

}
</pre>
<p>The password confirmation is implemented in lines 27-29. This part of the code tells Cake that we will be providing the Model with two different fields (&#8220;password&#8221; and &#8220;password_confirm&#8221;) and that we would like to apply the function named &#8220;confirmPassword&#8221; listed bellow (lines 39-43). Have a look at the relevant manual page (&#8220;<a  href="http://book.cakephp.org/view/152/Adding-your-own-Validation-Methods" title="CakePHP manual">Adding your own Validation Methods</a>&#8220;) to read how you can use your own functions for advanced field validation.</p>
<p>Go ahead ahead and play around with the code provided. Bake your own Controller and see if you can create some forms which will allow you to add new users and edit the existing ones.</p>
<p>~~~to be continued&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://pournaras.zilsen.com/2009/03/29/cakephp-auth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

