Here Kohana is complaining about some method that does not exist. Why? Well now may be the time to explain how routing usually works in MVC applications. When you request the page “http://example.com/index.php/welcome/hello” Kohana looks for the welcome controller (controller_welcome), if it does not find it it will throw the first exception (“Class controller_welcome does not exist”). If it does find it it will then look for the hello method in the welcome controller (action_hello) and it it does not find that it will throw the second exception (“Method action_hello does not exist”).

What about arguments? What if you want to show a certain message like this “http://example.com/messages/show/54632“? In a case like this the default routing behavior if to pass the last part of the URI as an argument to the action method. The above request would thus result in the following call “controller_messages::show(54632)”.

If you have a look in your application folder at “[application_root]/application” you will see the following:

We must place all our controllers in “[application_root]/application/classes/controller/”. As you can see there is already a file name “welcome.php” in there. This makes sense because as we saw above the welcome controller already seems to be implemented. So let’s create our messages controller. Create a new file named “messages.php” in the “classes/controller/” folder. You will get a feel for the file placement convention as you go along. Here is the code for the messages controller:

is simply there to avoid direct access to this file (this should never happen, but it is an extra security measure). Make sure you put this at the top of all your files.

Next we define the class:

class Controller_Messages extends Controller {

This if the Kohana Naming convention (actually it also has something to do with where you place your files, but we will get to that a bit further down the road). Here we have created a class “Controller_Messages” that extends “Controller” (part of the Kohana core). Next we define out method (“messages/show/”):

The method takes an argument (“messages/show/9548″) or makes it NULL if none is passed (“messages/show/”). The rest of the code is pretty self explanatory. Now try going to “http://localhost/messages/show/” and then “http://localhost/messages/show/439834“. Hopefully everything will work as expected. Here you have seen the most basic way of creating a controller and a corresponding request method.

One last thing. What if you go to “http://localhost/messages/showall“? Well it will throw a “method not found” exception and that makes sense. But what about “http://localhost/messages/“? That will also throw the above exception, but how should we name this method? Well this depends on the framework convention again: in kohana if no is given in the URI the default method “action_index” will be called. We have not defined this yet. So let’s quickly do that. Here is the final code for the messages controller:

Try it out and see if it works. Hopefully it does . This is meant to be a short introduction to the basic use of controllers in MVC frameworks, which you will need to be able to follow the next parts of this tutorial series.