Kohana PHP 3.0 (KO3) Tutorial

November 21, 2009
Filed under: Featured, PHP, Web 3.0 

So you might have read my aricle on frameworks and/or my series of tutorials on Kohana PHP 2.3.x and you are wanting more. Today, I drop the old 2.3.x and bring the new and shiny! So I bring you information to get you started with Kohana PHP 3.0!

Lets check to make sure we have everything needed before going on.

Lets go!
Download:
Download the latest Kohana 3.0 PHP (At the time of this writing: 3.0.1.2) and unpack it somewhere.

Install:
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 “kohana” or something like that. Open that folder. Open a new window and open the root directory of your *AMP install. Since I’m using WAMP Server – mine is “C:\wamp\www\”. Next make a new folder in there named “mykohana3″. Copy the files from the “kohana” directory to the “mykohana3″. Make sure your *AMP installation is up and running then point your browser to “http://yourserver/mykohana3/”. You should have a screen stating that everything is “OK”.

If everything is “OK”, then remove or rename the “install.php” file in the “mykohana3″ directory. Next open up the “example.htaccess” file and change the following line:

RewriteBase /kohana/

to:

RewriteBase /mykohana3/

Save it as “.htaccess”.

Now open the “bootstrap.php” file located in the “application” folder and cange the following line:

Kohana::init(array('base_url' => '/kohana/'));

to:

Kohana::init(array('base_url'  => '/mykohana3/',
                   'index_file'=> ''));

Save this file then refresh your browser. You should get something that reads “hello, world!” on your screen.

You might already notice that configuration for KO3 is a little bit more involved, editing two files instead of one, which isn’t a big deal at all.

Now to make our first controller! Open a new document and put the following into it:

<?php
defined('SYSPATH') or die('No direct script access.');

class Controller_Ko3 extends Controller
 {
    public function action_index()
     {
        $this->request->response = 'My First Kohana 3.0 Controller';
     }
 } // End

Save this as “ko3.php” in the “application/classes/controller” folder. You might have noticed another difference between Kohana 2.3.x and 3.0 is the directory structure, not really all that much of difference. Now that you have it saved, point your browser to “http://yourhost/mykohana3/ko3″. You should she “My First Kohana 3.0 Controller” on your screen now.

Now for an explanation of the code.




defined('SYSPATH') or die('No direct script access.');

This line basically tells PHP not load this file directly. It can only be included from the framework.

class Controller_Ko3 extends Controller

This creates an controller which is a class that is extended from the Controller class that is part of the framework.

public function action_index()

This created a public method called “action_index”. The “action_index” method is a default action that is loaded by the framework. It’s like your index.php file so to say.

$this->request->response = 'My First Kohana 3.0 Controller';

This will output “My First Kohana 3.0 Controller” to the screen. This basically works like “echo”.

Pretty easy eh? Now if you wanted to add addition action to your controller you would add another public method that has a prefix of “action_” and the you would access via going to “http://yourserver/mykohana3/controller/action”

Let go ahead and add a new method to our “ko3″ controller by adding the following after the “action_index” method:

    public function action_another()
     {
        $this->request->response = 'Another action';
     }

Save the file and loaded up “http://yourserver/mykohana3/ko3/another” in your browser. If all goes well you should see “Another action” in your browser.

That was fun an all, but lets make it a little bit more dynamic!

Copy this code and put it after the “action_another” method:

    public function action_dynamic($say)
     {
        $this->request->response = 'You said: '.$say;
     }

Save this and load “http://yourserver/mykohana3/ko3/dynamic/Monkey” and you should see “You said: Monkey”

Untill next time, when I will go over the first part of views, happy coding!
Sources used: Unofficial Kohana 3 Wiki

Comments

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!





*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image