<?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>Inside DealTaker &#187; PHP</title>
	<atom:link href="http://www.dealtaker.com/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dealtaker.com/blog</link>
	<description>All Things Deal Oriented</description>
	<lastBuildDate>Fri, 20 Nov 2009 21:47:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Kohana PHP Tuturial &#8211; Part III</title>
		<link>http://www.dealtaker.com/blog/2009/06/19/kohana-php-tuturial-part-iii/</link>
		<comments>http://www.dealtaker.com/blog/2009/06/19/kohana-php-tuturial-part-iii/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 13:26:09 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[technology]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=1013</guid>
		<description><![CDATA[It&#8217;s been a couple weeks now, so lets get back on track with this series of tutorials. This tutorial will get into models and how to play with data.

The first thing I have to is tell you is that Kohana PHP has been updated since the last tutorial. So I have gone ahead and took [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a couple weeks now, so lets get back on track with this series of tutorials. This tutorial will get into models and how to play with data.<br />
<span id="more-1013"></span></p>
<p>The first thing I have to is tell you is that Kohana PHP has been updated since the last tutorial. So I have gone ahead and took the last tutorial and updated the core and made sure it worked with Kohana 2.3.4. <a href="http://images.dealtaker.com/dealtaker/blog/kohana/dealtaker-kohana-2-2.3.4.zip">You can grab that here</a>. Now onward to the glory!</p>
<p>The first thing we want to do is identify where and what the data is. Is it an XML feed, CSV, JSON, DB or something else? Well I&#8217;m going to make it easy. We are going to deal with our friend MySQL for this. The next step is to setup a MySQL DB connection.</p>
<p>Go into your system/config folder and copy the database.php file to your application/config folder. Edit the application/config/database.php to reflect your server settings. Mine looks like this:</p>
<pre style="white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;">$config['default'] = array
(
	'benchmark'     =&gt; TRUE,
	'persistent'    =&gt; FALSE,
	'connection'    =&gt; array
	(
		'type'     =&gt; 'mysql',
		'user'     =&gt; 'root',
		'pass'     =&gt; 'root',
		'host'     =&gt; 'localhost',
		'port'     =&gt; FALSE,
		'socket'   =&gt; FALSE,
		'database' =&gt; 'myfirstkohana'
	),
	'character_set' =&gt; 'utf8',
	'table_prefix'  =&gt; '',
	'object'        =&gt; TRUE,
	'cache'         =&gt; FALSE,
	'escape'        =&gt; TRUE
);</pre>
<p>Now that we have a configuration a connection for our database, we should probably set up a database and a table. Here&#8217;s the SQL:</p>
<pre style="white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;">/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`myfirstkohana` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `myfirstkohana`;

/*Table structure for table `posts` */
DROP TABLE IF EXISTS `posts`;

CREATE TABLE `posts` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `title` varchar(255) default NULL,
  `post` text,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;

/*Data for the table `posts` */
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;</pre>
<p>This is a pretty straight forward &#8220;Create a DB and one table&#8221; setup. You might notice that we have everything set for UTF8. This will match the DB configuration setting and also will allow us to deal with i18n (Internationalization) stuff later on.</p>
<p>Onward to creating an actual model. There are a couple things to keep in mind with naming conventions, well really one. From the docs &#8220;The model class name is capitalized, does have _Model appended to it and should be the singular form of the name.&#8221;. There are so other rules when you are dealing with ORM, but we won&#8217;t be dealing with ORM in this tutorial.</p>
<p>Create a file named post.php in the &#8216;application/models/&#8217; folder and make it look like the following:</p>
<pre style="white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;">&lt;?php
defined('SYSPATH') or die('No direct script access.');

class Post_Model extends Model
 {
    public function __construct()
    	{
		      parent::__construct();
    	}

 }</pre>
<p>As you can tell the above really doesn&#8217;t do much, so let&#8217;s give it some functionality:</p>
<pre style="white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;">&lt;?php
defined('SYSPATH') or die('No direct script access.');

class Post_Model extends Model
 {
    public function __construct()
    	{
		      parent::__construct();
    	}

    public function getPosts()
     {
        $sql = 'SELECT *
                FROM   `posts`
                LIMIT  0, 10';

        return $this-&gt;db-&gt;query($sql);
     }
 }</pre>
<p>So we have a model method that pulls 10 posts from the table with a pretty simple query. We use &#8220;$this-&gt;query()&#8221; to run queries, which will return object and we return that to the calling entity. Check the query method <a href="http://docs.kohanaphp.com/libraries/database/query" target="_blank">docs here</a> for more information.</p>
<p>Let&#8217;s update our &#8216;Hello&#8217; controller to use this the model.</p>
<p>In your application/controllers/hello.php update your index() method to look like this:</p>
<pre>    public function index()
     {
        // Load the models
        $post  = new Post_Model;
        $posts = $post-&gt;getPosts();
        $rpsts = "";

        // Loop thru the posts
        foreach($posts-&gt;result_array(FALSE) as $row)
         {
          // Simple output of
          $rpsts .= '
    &lt;h1&gt;'.$row['title'].'&lt;/h1&gt;
    '.$row['post'].'&lt;hr /&gt;';
         }

        // Put something useful in our variables.
        $this-&gt;template-&gt;header-&gt;pageTitle .= ' ::: I am on the top';
        $this-&gt;template-&gt;content-&gt;content   = $rpsts;
     }</pre>
<p>When we run the hello controller, we won&#8217;t get much but &#8216;This is my second view&#8217;. We need to put stuff in the table. Use your favorite method of accessing your DB and insert some rows. Now when you run it you will see stuff!</p>
<p>If you notice there is a &#8220;$post-&gt;result_array()&#8221; inside a foreach loop. This allows us to loop though the results of the query easily from within the controller.</p>
<p>You might have notice something of bad practice. I pretty much created HTML inside the controller. As we all know, we shouldn&#8217;t do this. Let&#8217;s fix this!</p>
<p>Create a new view named main_posts.php and make it look like:</p>
<pre style="white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;">&lt;?php foreach($posts as $post): ?&gt;
&lt;h1&gt;&lt;?php echo $post['title'];?&gt;&lt;h1&gt;
&lt;?php echo $post['post'];?&gt;&lt;hr /&gt;
&lt;?php endforeach; ?&gt;</pre>
<p>This view does a foreach on our query results and fills in our little view.</p>
<p>In the the index method of our controller we will need to change up the controller to use our new view.</p>
<pre style="white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;">   public function index()
     {
        // Load the models
        $post  = new Post_Model;
        $posts = $post-&gt;getPosts();

        // Put something useful in our variables.
        $this-&gt;template-&gt;header-&gt;pageTitle .= ' ::: I am on the top';

        // Posts view
        $this-&gt;template-&gt;content-&gt;content        = new View('main_posts');
        $this-&gt;template-&gt;content-&gt;content-&gt;posts = $posts-&gt;result_array(FALSE);
     }</pre>
<p>As you can tell, it&#8217;s pretty simple and it&#8217;s clean! Here&#8217;s a quick sample on how to do an insert. We are going to add a method to the post model called addPost.</p>
<pre style="white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;">  public function addPost($title, $post)
    {
       $sql = sprintf('INSERT INTO `posts`
                       SET         `title` = %s,
                                   `post`  = %s',
                       $this-&gt;db-&gt;escape($title),
                       $this-&gt;db-&gt;escape($post));
      $this-&gt;db-&gt;query($sql);
    }</pre>
<p>We need to add a method to handle adding of posts to our hello controller:</p>
<pre style="white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;">   public function addpost()
     {
        // Load the models
        $post  = new Post_Model;
        $post-&gt;addPost($_POST['title'], $_POST['post']);
        url::redirect('hello');
     }</pre>
<p>And lets add a form to the end of the main_post.php in the views:</p>
<pre style="white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;">&lt;form method="POST" action="&lt;?php echo url::base();?&gt;hello/addpost/"&gt;
  &lt;table&gt;
    &lt;tr&gt;
     &lt;th&gt;
       Title
     &lt;/th&gt;
     &lt;th&gt;
       Post
     &lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;td&gt;
      &lt;input type="text" name="title" /&gt;
     &lt;/td&gt;
     &lt;td&gt;
      &lt;textarea cols="20" rows="5" name="post"&gt;&lt;/textarea&gt;
      &lt;input type="submit" name="submit" value="Submit"/&gt;
     &lt;/td&gt;
  &lt;/table&gt;
&lt;/form&gt;</pre>
<p>Once again, pretty simple right? I could go on and give you how to edit entries, but I&#8217;m leaving that to you for your home work. Free free to post your results! Until next time when will go over libraries and helpers, keep on coding till your fingers bleed!</p>
<p><a href="http://images.dealtaker.com/dealtaker/blog/kohana/dealtaker-kohana-3.zip">Get the file for this tutorial here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/blog/2009/06/19/kohana-php-tuturial-part-iii/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Kohana PHP 2.3.x Tutorial Part II</title>
		<link>http://www.dealtaker.com/blog/2009/05/21/kohana-php-23x-tutorial-part-ii/</link>
		<comments>http://www.dealtaker.com/blog/2009/05/21/kohana-php-23x-tutorial-part-ii/#comments</comments>
		<pubDate>Thu, 21 May 2009 14:51:27 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[technology]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=633</guid>
		<description><![CDATA[Welcome to the second tutorial in this series on how to develop with Kohana PHP. If you haven&#8217;t read the first part, I would click here and read it before going on. In this tutorial we will be going over views, wonderful views, glorious views.

Let’s just dive straight into building our first picturesque view.
Create the [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the second tutorial in this series on how to develop with Kohana PHP. If you haven&#8217;t read the first part, I would <a href="http://www.dealtaker.com/blog/2009/04/23/kohana-php-23x-tutorial-part-1/">click here</a> and read it before going on. In this tutorial we will be going over views, wonderful views, glorious views.<br />
<span id="more-633"></span></p>
<p>Let’s just dive straight into building our first picturesque view.<br />
Create the file &#8220;myfirstkohana/application/views/hello.php&#8221; and put the following into it:</p>
<pre>&lt;html&gt;
 &lt;head&gt;
  &lt;title&gt;Hello!&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;
  &lt;h1&gt;This is my first view&lt;/h1&gt;
  &lt;?php echo $content;?&gt;
 &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Looking at the above, it&#8217;s pretty straight forward and simple HTML file with some simple PHP code thrown in.</p>
<p>Just because we put a view with the same name as our controller, it&#8217;s not going to be shown until we say so. Let’s go back to our  wonderful controller &#8220;myfirstkohana/application/controllers/hello.php&#8221; and edit the index method to look like the following:</p>
<pre>public function index()
 {
  $view          = new View('hello');
  $view-&gt;content = 'Hello World!';
  $view-&gt;render(TRUE);
 }</pre>
<p>Let go through this line by line. The first line:</p>
<pre>$view = new View('hello');</pre>
<p>tells the framework to create a new &#8220;view&#8221; object and load the file :myfirstkohana/application/views/hello.php&#8221; into it. The next line:</p>
<pre>$view-&gt;content = 'Hello World!';</pre>
<p>creates a new property called &#8220;content&#8221; in the object that has the value &#8220;Hello World!&#8221;. The last line</p>
<pre>$view-&gt;render(TRUE);</pre>
<p>compiles the template with our assigned values and outputs it to the screen. What we should get when we load &#8220;http://youserver/myfirstkohana/hello&#8221; is &#8220;This is my first view&#8221; and below that should read &#8220;Hello World!&#8221;.</p>
<p>This might the simplist view you&#8217;ll see. Of course, I don&#8217;t think we&#8217;ll be wanting something so simple, so lets knock it up notch and create views with nested views.</p>
<p>Create the file &#8220;myfirstkohana/application/views/default_template.php&#8221; and put this in it:</p>
<pre>&lt;?php echo $header; ?&gt;
&lt;?php echo $content; ?&gt;
&lt;?php echo $footer; ?&gt;</pre>
<p>The above code is just a master template, or the template of templates if you will. Next lets create &#8220;myfirstkohana/application/views/default_header.php&#8221; and put the following into it:</p>
<pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" &gt;
 &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;title&gt;&lt;?php echo $pageTitle;?&gt;&lt;/title&gt;
 &lt;/head&gt;</pre>
<p>and then &#8220;myfirstkohana/application/views/default_content.php&#8221;:</p>
<pre> &lt;body&gt;
  &lt;h1&gt;This is my second view&lt;/h1&gt;
  &lt;?php echo $content;?&gt;</pre>
<p>finally &#8220;myfirstkohana/application/views/default_footer.php&#8221;:</p>
<pre> &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Above we taken a basic HTML page and split it up &#8220;logically&#8221; into a header, footer and a content section. Let open our controller to build out our more complex view.  Edit &#8220;myfirstkohana/application/controllers/hello.php&#8221; to make the index method look like this:</p>
<pre>  public function index()
   {
    $view                    = new View('default_template');
    $view-&gt;header            = new View('default_header');
    $view-&gt;content           = new View('default_content');
    $view-&gt;footer            = new View('default_footer');
    $view-&gt;header-&gt;pageTitle = 'I am on the top';
    $view-&gt;content-&gt;content  = 'Hello World!';

    $view-&gt;render(TRUE);
   }</pre>
<p>That wasn&#8217;t too hard was it? Basically we load our first view, which is the main template. Then we populate our properties with objects of our inner templates. It&#8217;s like working with multi-demential arrays in a sense.</p>
<p>With the ability to nest views/templates inside each other, things are a lot nicer to deal with. There is a problem thou. Each method (action) that needs to have a templated view, you need to do what we did to our index action to each one. Kohana does have an answer.</p>
<p>The Template_Controller allows to give a default view to actions, which if you have a lot of actions for a controller, it can really cut down on development and debugging time. Let go ahead and re-work out the hello controller (&#8221;myfirstkohana/application/controllers/hello.php&#8221;).</p>
<pre>&lt;?php
defined('SYSPATH') or die('No direct access allowed.');

class Hello_Controller extends Template_Controller
 {
    public $template    = 'default_template'; // Default template (view) to use
    public $auto_render = TRUE;               // Auto render template after controller is done

    public function __call($method, $arguments)
     {
        $this-&gt;name($method);
     }

    public function __construct()
     {
        parent::__construct(); // Has to be here!

        // Load the inner templates
        $this-&gt;template-&gt;header            = new View('default_header');
        $this-&gt;template-&gt;content           = new View('default_content');
        $this-&gt;template-&gt;footer            = new View('default_footer');
        $this-&gt;template-&gt;header-&gt;pageTitle = 'My first Kohana App';
     }

    public function index()
     {
        // Put something useful in our variables.
        $this-&gt;template-&gt;header-&gt;pageTitle .= ' ::: I am on the top';
        $this-&gt;template-&gt;content-&gt;content   = 'Hello World!';
     }

    public function america()
     {
        $this-&gt;template-&gt;content-&gt;content = 'Hello America!';
     }

    public function name($name)
     {
        $this-&gt;template-&gt;content-&gt;content = 'Hello ',$name,'!';
     }
 }</pre>
<p>Here we have defaulted our main template, so now we didn&#8217;t have to declare it in each method. Also in our constructor method, we loaded the sub templates in and gave a default value to one of the template variable that used in the title tag. In our index method we added on to the title tag (think SEO) and put some generic content in place.</p>
<p>Stay tuned for the third part of our series and find out about models and working with external data!<br />
<a href="http://images.dealtaker.com/dealtaker/blog/kohana/dealtaker-kohana-2.zip">Click here to download the files</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/blog/2009/05/21/kohana-php-23x-tutorial-part-ii/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Kohana PHP 2.3.x Tutorial Part 1</title>
		<link>http://www.dealtaker.com/blog/2009/04/23/kohana-php-23x-tutorial-part-1/</link>
		<comments>http://www.dealtaker.com/blog/2009/04/23/kohana-php-23x-tutorial-part-1/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 15:22:25 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[controllers]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=558</guid>
		<description><![CDATA[So after reading my article about frameworks, you might want to try one out. Today we are going start a series of tutorials on how to use the Kohana PHP framework beginning with how to install it and creating a controller.

Before we get into this tutorial, lets go over what will be required.

*AMP (Apache MySQL [...]]]></description>
			<content:encoded><![CDATA[<p>So after reading <a href="2009/02/10/frameworks-frameworks-frameworks/">my article about frameworks</a>, you might want to try one out. Today we are going start a series of tutorials on how to use the Kohana PHP framework beginning with how to install it and creating a controller.<br />
<span id="more-558"></span></p>
<p>Before we get into this tutorial, lets go over what will be required.</p>
<ul>
<li>*AMP (Apache MySQL PHP) install</li>
<li>Knowledge of PHP</li>
<li>Know what a frame work is (<a href="http://www.dealtaker.com/blog/2009/02/10/frameworks-frameworks-frameworks/">Framework Article</a>)</li>
<li>Know what MVC is (<a href="http://en.wikipedia.org/wiki/Model-view-controller">Wikipedia Entry</a>)</li>
</ul>
<p>Lets get started!<br />
Download:<br />
Load <a href="http://www.kohanaphp.com/">http://www.kohanaphp.com</a> in your favorite browser and click the <a href="http://www.kohanaphp.com/download">download link</a>. On this page, click the &#8220;<a href="http://www.kohanaphp.com/download?languages%5Ben_US%5D=en_US&amp;format=zip">Download Kohana!</a>&#8221; button. You should be prompted to save a file. Go a head and save the file some where. At the time of this writing, the version was at 2.3.2.</p>
<p>Install:<br />
Open the file we just downloaded in your favorite archive program and extract it to a temporary location. Open that temporary location and you should have a folder that is named &#8220;Kohana_v2.3.2&#8243; or something like that. Open that folder. Open a new window and open the root directory of your *AMP install. Since I&#8217;m using WAMP Server &#8211; mine is C:\wamp\www\. Next make a new folder in there named &#8220;myfirstkohana&#8221;. Copy the files from the &#8220;Kohana_v2.3.2&#8243; directory to the &#8220;myfirstkohana&#8221;.  Make sure your *AMP installation is up and running then point your browser to http://yourserver/myfirstkohana/ . You should have a screen stating that everything is &#8220;OK&#8221;.<br />
<img src="http://images.dealtaker.com/dealtaker/blog/kohana/kohana-1-1.png" alt="" /></p>
<p>If everything is &#8220;OK&#8221;, then remove or rename the &#8220;install.php&#8221; file in the &#8220;myfirstkohana&#8221; directory. Next we want to navigate into the application folder, then in the the config folder. Open the file named &#8220;config.php&#8221; in your favorite editor. There should be a line that reads: &#8220;$config['site_domain'] = &#8216;/kohana/&#8217;;&#8221; (Line 7 for me). We need to change the &#8220;/kohana/&#8221; part to reflect our install. So replace &#8220;/kohana/&#8221; with &#8220;/myfirstkohana/&#8221;. Also search for &#8220;$config['index_page'] = &#8216;index.php&#8217;;&#8221; and make the &#8220;index.php&#8221; part blank, so that line (Line 21 for me) should now read &#8220;$config['index_page'] = &#8221;;&#8221;.  Save that file.</p>
<p>Create a new document, cut and paste the following into it:</p>
<pre># Turn on URL rewriting
RewriteEngine On

# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/kohana/, use /kohana/
RewriteBase /myfirstkohana/

# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]</pre>
<p>Save this file in the &#8220;myfirstkohana&#8221; directory as &#8220;.htaccess&#8221;.</p>
<p>These changes will help with creating clean URLs that help with SEO efforts.</p>
<p>Reload the browser page and you should get something like this:<br />
<img src="http://images.dealtaker.com/dealtaker/blog/kohana/kohana-1-2.png" alt="" /></p>
<p>If everything looks good, then we have successfully installed Kohana PHP.  Now lets make it do stuff!</p>
<p>My first controller:<br />
Open a new document and put the following into it:</p>
<pre>&lt;?php
defined('SYSPATH') or die('No direct access allowed.');

class Hello_Controller extends Controller
 {
  public function index()
   {
    echo 'Hello World!';
   }
 }</pre>
<p>Save this in &#8220;myfirstkohana/application/controllers&#8221; as &#8220;hello.php&#8221;. Here&#8217;s a little explaination of what we did. The line &#8220;defined(&#8217;SYSPATH&#8217;) or die(&#8217;No direct access allowed.&#8217;);&#8221; basically prevents the file from being executed by itself. The next line &#8220;class Hello_Controller extends Controller&#8221; creates a class called &#8220;Hello_Controller&#8221; that extends the controller object. The rules to follow for creating controllers is as follows:</p>
<ul>
<li>Must reside in a controllers (sub-)directory</li>
<li>Controller filename must be lowercase, e.g. articles.php</li>
<li>Controller class must map to filename and capitalized, and must be appended with _Controller, e.g. Articles_Controller</li>
<li>Must have the Controller class as (grand)parent</li>
<li>Controller methods preceded by &#8216;_&#8217; (e.g. _do_something() ) cannot be called by the URI mapping</li>
</ul>
<p>stolen from: <a href="http://docs.kohanaphp.com/general/controllers">Kohana Docs</a></p>
<p>The first method, &#8220;index&#8221; is the default action. Pretty easy right? If you point your browser to &#8220;http://yourserver/myfirstkohana/hello/&#8221; you should see &#8220;Hello World!&#8221; on the screen now.</p>
<p>What if you wanted to exend the controller to do something else? Let add another method in the class that looks like:</p>
<pre>public function america()
 {
  echo 'Hello America!';
 }</pre>
<p>Now the hello.php should look like:</p>
<pre>&lt;?php
defined('SYSPATH') or die('No direct access allowed.');

class Hello_Controller extends Controller
 {
  public function index()
   {
    echo 'Hello World!';
   }

  public function america()
   {
    echo 'Hello America!';
   }
 }</pre>
<p>Save this and open &#8220;http://yourserver/hello/america/&#8221;. Notice that is now show &#8220;Hello America!&#8221; on the screen. That wasn&#8217;t too hard, but lets make this a little more dynamic with arguements. Make a new method that will look like this:</p>
<pre>public function name($name)
 {
  echo 'Hello ',$name,'!';
 }</pre>
<p>Load up &#8220;http://yourserver/hello/name/monkey&#8221; and you should see &#8220;Hello monkey!&#8221; on your screen. Pretty straight forward. Let make it even more &#8216;friendly&#8217;. Add this to the top of the class:</p>
<pre>public function __call($method, $arguments)
 {
  $this-&gt;name($method);
 }</pre>
<p>If we load up &#8220;http://yourserver/myfirstkohana/hello/MrGuy/&#8221; we should get the same thing as if we loaded &#8220;http://yourserver/myfirstkohana/hello/name/MrGuy/&#8221; which would be &#8220;Hello MrGuy!&#8221;.</p>
<p>That&#8217;s it for this lesson. Next time we&#8217;ll be looking over Views.<br />
<a href="http://images.dealtaker.com/dealtaker/blog/kohana/dealtaker-kohana-1.zip">Click here</a> to download the files and don&#8217;t forget to check out some <a title="Tech Deals" href="http://www.dealtaker.com/tech-deals.html">great computer and electronic deals</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/blog/2009/04/23/kohana-php-23x-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
