Kohana Framework 3 Tutorial part 2 – quick introduction to controllers and the framework

Before building the actual Twitter application I think it would be useful to go over the way the framework works by default. So right now we have a clean and working install. Let’s see how it works.
If you go to “http://localhost/” or “http://localhost/index.php” you should see a page that just says “hello, world!”. That is our default/index page. So what happens if you go to “http://localhost/index.php/hello/“? You will get an ugly exception that looks like this:



wpid-kohana_controller_hello_does_not_exist.png
It seems like Kohana cannot find the “Hello” controller (controller_hello). What about if you go to “http://localhost/index.php/welcome“? Strange…we get the default/index page. So it seems like the “http://localhost/index.php/welcome” is actually the same as “http://localhost“. This has something to do with your default route. We will go more into routing in the next part of the tutorial. At least we have a working controller to play with (controller_welcome). So let’s try this: “http://localhost/index.php/welcome/hello“, what happens? We get another ugly exception:
wpid-kohana_method_action_hello_does_not_exist.png
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”).
So the default routing behavior is like this: “http://example.com/index.php/<controller>/<action>”. If you want people to be able to request the page “http://example.com/index.php/messages/show” you would have to create the messages controller (controller_messages) and a show method (action_show), to avoid getting exceptions.
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)”.
We cam summarize again that the default behavior is this: “http://example.com/index.php/<controller>/<action>/<argument>”.
Hopefully this makes sense. Let’s try this by creating the necessary code to be able to respond to the above request: “http://example.com/messages/show/54632“.
If you have a look in your application folder at “[application_root]/application” you will see the following: wpid-kohana_application_classes_controller.png
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:


<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Messages extends Controller {

 public function action_show($id = NULL)
 {
  if($id == NULL)
   echo "no argument given";
  else
   echo "show message with id: $id";
 }

} // End Messages

The first line

<?php defined('SYSPATH') or die('No direct script access.');

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/”):

public function action_show($id = NULL)
{
 if($id == NULL)
  echo "no argument given";
 else
  echo "show message with id: $id";
}

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:


<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Messages extends Controller {

 public function action_index()
 {
  echo "no <action> given";
 }

 public function action_show($id = NULL)
 {
  if($id == NULL)
   echo "no argument given";
  else
   echo "show message with id: $id";
 }

} // End Messages

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.


http://o1iver.net/blog/2010/05/kohana-v3-tutorial-part-2-quick-introduction-to-the-framwork/

0 comments:

Post a Comment