“Kohana is an elegant HMVC PHP5 framework that provides a rich set of components for building web applications.” (kohanaframework.org).
I am a big fan of web application framework as they really do cut down on some of the development time. However they can be quite challenging to the newcomer. A few years ago I first started with CakePHP and then quickly moved to CodeIgnigter, which has been my framework of choice until I found Kohana. It is by far the best framework out there, not only because it is pure PHP5, but also because it offers an unprecedented amount of flexibility to the user.
Anyway lets not talk about the theory but rather about this tutorial. Because Kohana’s documentation is pretty bad, nonexistent for version 3 I though I would do a few tutorial series here. I will try to build an entire web application (nothing too fancy, more on that later) and document every step here. I am not a PHP expert, nor a Kohana expert, but I hope this will provide beginners with a bit of a head-start!
The application: since I couldn’t come up with a better idea I thought I would just recreate Twitter. I personally like the way Twitter is built (especially the sexy URLs) so I thought this would be a good example application.
So lets start with Part 1:
We will cover the basic installation of the framework and configuration of you environment (sounds complicated, buts it really easy).
So first you will have to download the newest version of the framework from http://kohanaframework.org (I am going to be using v220.127.116.11 for this tutorial). You should have a file called “kohana-18.104.22.168.zip”. Unzip this file and copy its contents into the root directory of your application. This is the directory that your server will look into when accessed through your application’s domain (in this tutorial that will be http://localhost/, but for our real application this would be http://twitter.com).
Here is a screenshot of what I have in my web application’s root directory:
Just so you get a quick idea: the “application” folder is where we will be building out Twitter application, “modules” contains helper classes (this will be explained in more detail in a future part as they are not of too much interest to us now) and “system” contains the Kohana framework code (feel free to check it out, but make sure you know what you are doing before you start changing things in there).
So…let’s get started. If you now point your browser so http://localhost/ you will get the Kohana “Environment Tests” page. It displays some information about your PHP version, you directories, etc… My test never passes at first because of two things and I guess this will be a problem for many people. Here is the screenshot:
Two problems: the cache and the logs directory are not writable. The cache directory is where Kohana will save cache data on the fly (if you don’t want to cache anything I guess this will not be a problem) and the logs directory is where Kohana will produce your log files. These are very useful for debugging during development (although most errors will throw exceptions and print a stack trace in the browser), but these are even more important in a production environment, so that you can check your custom log messages (yes, you can print messages to the log in your application manually) and any other system errors. When in production we will turn off the in-browser-error-reporting as we do not want the user to be confronted with ugly error messages, but we would still like to be able to check from time to time and see what kind of errors the users are getting.
How do we fix this?
I use Mac and Linux, so I will give you the solution for these systems here, unfortunately Windows users will have to play around themselves to fix this, but it should not be too hard (change the permissions of these two folders so that the web-server-user has read/write rights on them). For Mac/Linux, just enter these two commands in the terminal:
chmod -R 777 /Code/Twitter/application/logs/
chmod -R 777 /Code/Twitter/application/cache/
You will obviously have to change the directories to the ones in your configuration and depending on the ownership of these folders you may have to place “sudo” in front of these commands (makes you root for the following command) and enter your password.
Finally reload the page at http://localhost/ and you should (hopefully) see something like this:
That looks good! If you also pass the environment tests (or want to ignore the cache and log problems) it is now time to rename the install.php file (or delete it if you want).
In the next part of this tutorial I will configure the bootstrap.php file and introduce the basic application flow!