Back Home

Starting Kohana Development

Posted on

Intro

This is the first post in a new series on how to develop applications using the Kohana HMVC framework 3.0+. If you are unfamiliar with php, you should learn how to program with php first.

Today we are going to go over simple installation and configuration so that we have something to work with. I’m going to try and dumb it down for you guys so we can get some real work done real quick.

Step 1

Go ahead and visit the Kohana website and download the latest version.
Download Kohana Here

Once you have it downloaded you are going to want to extract the archive and place all the files on your server. I will be using a localhost server named WAMP. Place the folder called “Kohana” in the root directory of your server. My root folder is called www. So to clarify you will find the Kohana directory at C:\wamp\www\kohana.

Now in your web browser (i’m using Firefox) navigate to http://localhost/kohana, and what you should see is Environment Tests and a big list of tests, hopefully it looks like the image below. If you are this far, go ahead and do what it says and remove kohana/install.php. So this sums up step 1, and we are actually close to being done already.

Step 2

For step 2 we are going to open up the kohana folder and find the kohana/application/bootsrap.php file, this is the main configuration file. Go to about line 52 and find:

52
53
54
Kohana::init(array(
	'base_url'   => '/'
));

and change that to look like this:

52
53
54
55
Kohana::init(array(
	'base_url'   => '/kohana',
        'index_page' => ''
));

What we did here is told Kohana that our base_url is /kohana so the full url would look like this http://localhost/kohana , and for the index_page we dont want anything because we will be using an .htaccess file to rewrite our urls. So normally a Kohana url would look like this http://localhost/kohana/index.php/welcome and we will change it to look like http://localhost/kohana/welcome. This is not only better to look at but helps with SEO purposes.

Next, in your kohana folder, find a file called example.htaccess and open it. Notice the code shown below is at the very bottom of the file.

21
RewriteRule .* index.php/$0 [PT]

Currently, there is a problem with the way this rewrites so we will have to change that line to this:

21
RewriteRule ^(.+)$ index.php?kohana_uri=$1 [L]

Then rename the example.htaccess file to .htaccess.

Lastly, in the bootstrap file, we are going to find

87
88
89
90
91
Route::set('default', '(<controller>(/<action>(/<id>)))')
	->defaults(array(
		'controller' => 'welcome',
		'action'     => 'index',
	));

We aren’t going to do anything with this today, but ill tell you what it is for. The controller is exactly what it sounds like. It controls the page requests and data passed to each page. The action part is the default method that the controller will call. A sample url using the current route style http://localhost/kohana/controller/action/id. As of now, if you go to http://localhost/kohana/ you should see simply hello, world!.

Conclusion

That concludes the first Kohana tutorial. I hope you have learned how to successfully install the Kohana framework. Once we get into the more advanced tutorials, I’m sure you will grow more excited about using Kohana as it is a powerful and fast frame work; not bloated like Zend or others.
Next time, I will be going over the concepts of controllers and we will be building a very simple web page.

  • http://zackperdue.com Zackperdue

    This is a sweet article!

  • Pingback: What is a Controller | Zack Perdue

  • http://twitter.com/r1n0z33n r1n0z33n

    good one .. your tutorial looks very neat… i’ll try this later… 

    • Zack

      They have changed some of the code since the writing of this tutorial. let me know if you need any help.

  • LexLythius

    A readability tip: using “font-weight: 200″ almost makes the text disappear, it’s very hard to read in Chrome (Ubuntu at least), in Firefox it’s nearly impossible.

  • alan

    Zack, thank you so much for your tuts on Kohana 3 – I am migrating from Codeignitor and struggling to find good tutorials like yours! many thanks for your time!

    • http://zackperdue.com/ Zack Perdue

      I’m glad I could help Alan. I love using Kohana since it is all object oriented and much simpler in my opinion. Let me know if you would like to see any more tutorials. I need some sort of motivation to create some more! It has been a while.