Kohana PHP 3.0 (KO3) Tutorial Part 1
Posted by ellisgl on November 20, 2009
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.
- *AMP (Apache MySQL PHP) install
- Knowledge of PHP
- Know what a frame work is (Framework Article)
- Know what MVC is (Wikipedia Entry)
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
Rivoot said,
Thanks for the tutorial! Already waiting for the second part
.
pcdinh said,
> 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.
Why do you need this? It makes nonsense to me.
Caspar said,
Nice little tutorial. But there’s a little error in there: at first you mention ‘myfirstkohana3′ (RewriteBase in htaccess), then later on, this changes into ‘mykohana3′ (base_url in bootstrap).
ellisgl said,
@Caspar: I’ll get that fixed, sorry for the typo.
@Pcdinh: A controller must be loaded in by the framework to work properly, so if it’s not loaded, just stop the script and not generate a fatal errors on screen or in your logs.
Designerfoo said,
Great tutorial
When’s the second part coming out?
anonymous said,
Part 2! part 2!
ellisgl said,
Part two: http://www.dealtaker.com/blog/2009/12/07/kohana-php-3-0-ko3-tutorial-part-2
Hari K T said,
Wow cool . Easy to learn
.
wk3368 said,
on windows,
.htaccess won’t work until you unannotate this line in C:\xampp\apache\conf\httpd.conf :
#LoadModule rewrite_module modules/mod_rewrite.so
and restart apache.
Timothy said,
i cant seem to do this with .htaccess running? any ideas? i tried what wk3368 did but with no avail.. im running windows with AppServ 2.5.9.. i cant do anything bec it says internal server error.
ellisgl said,
Timothy: Check in the httpd.conf for
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride all
The “AllowOverride all” needs to be uncommented
Timothy said,
Thank you guys! onto part 2 keep this tut coming!
ellisgl said,
No problem Timothy, happy learning!
Timothy said,
if the method is dynamic it will get the value on the uri in the segment after the name of the method right? what if there are more values that i want to pass on the method?
Cris said,
Hi, I have the same problem with timothy then I tried the solution of ellisgl still doesnt work. Im using Appserv 2.5.9 Windows XP, any ideas?
ellisgl said,
Cris – Check with Appserv about .htaccess stuff.
ellisgl said,
Timothy: It should ignore the rest iirc.
Guillaume said,
Hello,
I downloaded the version 3.0.3 ( http://dev.kohanaphp.com/projects/kohana3/files ) and i followed your tutorial.
But, when i want to create de Controller_Ko3 and point my browser to the ko3 controller, i have a 404 error.
I tried this : http://kerkness.ca/wiki/doku.php?id=removing_the_index.php but no success.
I am on WAMP & Windows 7.
Can you help me ?
Regards,
Guillaume
Stephan said,
hi , thank you very much, i’am from germany, so excuse me…my english is not very well
@ Guillaume
I can help you.
try this:
http://localhost//index.php//
Maybe the owner of the blog can improve the statement.
greetings and thank you very much.
Add A Comment