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.
Lets get started!
Download:
Load http://www.kohanaphp.com in your favorite browser and click the Download Kohana! 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.
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_v2.3.2″ 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:wampwww. Next make a new folder in there named “myfirstkohana”. Copy the files from the “Kohana_v2.3.2″ directory to the “myfirstkohana”. 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 “OK”.

If everything is “OK”, then remove or rename the “install.php” file in the “myfirstkohana” directory. Next we want to navigate into the application folder, then in the the config folder. Open the file named “config.php” in your favorite editor. There should be a line that reads: “$config['site_domain'] = ‘/kohana/’;” (Line 7 for me). We need to change the “/kohana/” part to reflect our install. So replace “/kohana/” with “/myfirstkohana/”. Also search for “$config['index_page'] = ‘index.php’;” and make the “index.php” part blank, so that line (Line 21 for me) should now read “$config['index_page'] = ”;”. Save that file.
Create a new document, cut and paste the following into it:
# 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]
Save this file in the “myfirstkohana” directory as “.htaccess”.
These changes will help with creating clean URLs that help with SEO efforts.
Reload the browser page and you should get something like this:

If everything looks good, then we have successfully installed Kohana PHP. Now lets make it do stuff!
My first controller:
Open a new document and put the following into it:
<?php
defined('SYSPATH') or die('No direct access allowed.');
class Hello_Controller extends Controller
{
public function index()
{
echo 'Hello World!';
}
}
Save this in “myfirstkohana/application/controllers” as “hello.php”. Here’s a little explaination of what we did. The line “defined(‘SYSPATH’) or die(‘No direct access allowed.’);” basically prevents the file from being executed by itself. The next line “class Hello_Controller extends Controller” creates a class called “Hello_Controller” that extends the controller object. The rules to follow for creating controllers is as follows:
stolen from: Kohana Docs
The first method, “index” is the default action. Pretty easy right? If you point your browser to “http://yourserver/myfirstkohana/hello/” you should see “Hello World!” on the screen now.
What if you wanted to exend the controller to do something else? Let add another method in the class that looks like:
public function america()
{
echo 'Hello America!';
}
Now the hello.php should look like:
<?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!';
}
}
Save this and open “http://yourserver/hello/america/”. Notice that is now show “Hello America!” on the screen. That wasn’t too hard, but lets make this a little more dynamic with arguements. Make a new method that will look like this:
public function name($name)
{
echo 'Hello ',$name,'!';
}
Load up “http://yourserver/hello/name/monkey” and you should see “Hello monkey!” on your screen. Pretty straight forward. Let make it even more ‘friendly’. Add this to the top of the class:
public function __call($method, $arguments)
{
$this->name($method);
}
If we load up “http://yourserver/myfirstkohana/hello/MrGuy/” we should get the same thing as if we loaded “http://yourserver/myfirstkohana/hello/name/MrGuy/” which would be “Hello MrGuy!”.
That’s it for this lesson. Next time we’ll be looking over Views.
August 15, 2009 at 9:09 am
September 25, 2009 at 12:44 pm
November 9, 2009 at 7:56 am
November 23, 2009 at 8:16 am
March 13, 2010 at 2:58 pm
April 15, 2010 at 9:03 am
April 15, 2010 at 1:06 pm
April 19, 2010 at 12:40 am
April 19, 2010 at 7:36 am
August 17, 2010 at 11:08 am
September 20, 2010 at 5:47 am
September 20, 2010 at 8:31 am
January 18, 2011 at 1:30 am