Kohana Hello World
Kohana is quite and amazing tool to use in PHP application development. It’s an awesome MVC framework and can make development time way faster. I’m not getting into framework comparisons, as I mainly work in Kohana (when I’m not working in WordPress). This is a quick tutorial on a simple ‘hello world’ application (because every first time developer should always write a hello world app, it’s the rules).
Before we get started, go ahead and grab the latest version of Kohana here. I’ll be using v2.3.4.
1) Install Kohana
Which really means just copy the Kohana files into the directory you are going to be working in. For the sake of this tutorial, let’s put it in a directory name /helloworld/. Let the install.php script execute and tell you everything is good to go. I’m not addressing server errors in this tutorial (but I am assuming you’re on Apache). After everything is cleared, you can either rename the install.php or delete it.
2) Setup .htaccess
I usually go ahead and setup the .htaccess file. This way I don’t have to worry about troubleshooting this later. Kohana provides an example.htaccess file. Grab the contents of this and paste it into our .htaccess file in the root directory of the application (helloworld/). The only thing we are going to change is the line that reads ‘RewriteBase /kohana/’. We need this to say ‘RewriteBase /helloworld/’ (no quotes). This just tells the server what we will be running our application from the helloworld/ directory.
3) Create Our Controller
Kohana follows a strict naming convention for certain files in our application. For our ‘hello world’ application, we are just going to create a ‘hello’ controller. Create a file named hello.php in our /application/controllers/ directory. This is going to be the main controller for this application.
4) Start coding
Let’s jump in and start writing code and I will explain it as we go along.
First, add this line to the top of the hello.php (after our opening <?php tag, of course):
This prevents the files from accessing itself. You should always add this to each of your controllers.
Next, let’s go ahead and declare our class:
-
class Hello_Controller extends Controller{
-
-
}
This is where the naming convention can really be seen. Because we are declaring a controller for hello, the file name must be lower case. Also, the class declaration must match the filename, be capital case, and end in _Controller. Our class also extends the Controller class, which gives us access to all of Kohana’s core features for this class.
Example: a blog controller would have the filename ‘blog.php’, be declared as Blog_Controller, and extend Controller.
5) Add Index Function
Let’s go a head and add an index function to our Hello_Controller class.
-
class Hello_Controller extends Controller{
-
-
function index(){
-
-
echo ‘hello world!’;
-
-
}
-
-
}
I’ll cover this a little more in the next step.
6) Test It
At this point you should be able to access your controller. First, let’s look at how Kohana reads the url structure. This is an example of a basic call to a controller:
(application root)/controllerName/functionName
Therefore, we should be able to call our hello controller by accessing (your domain)/helloworld/hello/index. This calls the index function of our hello controller.
*hint* – you do not have to specify index in the url. If a function name is not given in the url, Kohana will look for the index function.
Accessing this function should give you our ‘hello world!’ output from step 5.
7) That’s it
This was a quick and easy example of how to get a simple application up and running. I’ll cover using views and models soon.
Tags: beginner, hello world, kohana, php, Programming, tutorial