What is a Controller

Posted on

Intro

Welcome to day 2 of Learning how to use Kohana. Today I’m going to show you what a controller is and how to use a controller. If you haven’t installed Kohana then you may view my first tutorial Starting Kohana Development. This will teach how how to install it, and once you have done that you will be ready to learn controllers.

What is a Route

First we need to understand how the HVC url format works. Take a look below. You will notice inside the array the word controller. Specifically, this part in the bootstrap.php file is setting the default controller. That means if you navigate to http://localhost/kohana/index.php/welcome, you will be calling the welcome controller. The part that says action is the default method that will be called inside the welcome controller. So currently, it is pointing to the index action. This means that if we navigate in our browser to http://localhost/kohana/ its going to automagially call our default controller and default method.

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

What is a Controller

What a controller is is basically a class that directs and shapes our requests and responses to the correct pages (views). You could think of it as a traffic director.

What we will do now is open up in our editor kohana/application/classes/controller/welcome.php and it should look like the code below.

1
2
3
4
5
6
7
8
9
10
<?php defined('SYSPATH') or die('No direct script access.');
 
class Controller_Welcome extends Controller {
 
	public function action_index()
	{
		$this->request->response = 'hello, world!';
	}
 
} // End Welcome

We are going to add a new method to the controller. So below

5
6
7
8
public function action_index()
	{
		$this->request->response = 'hello, world!';
	}

add this:

10
11
12
public function action_test(){
		$this->request->response = 'This is just a test ladies and gentleman.';
	}

What this is saying is that we want the page to basically echo This is just a test ladies and gentleman.. So now we will test it. navigate your browser to http://localhost/kohana/welcome/test. When we put test (action) after welcome/ (controller) like welcome/test we are calling the test method of the controller class. So now hopefully it says what we want it to.

Lets get Dynamic

Ok, we started out with a very basic controller that just outputted something on the page. Now lets add on to that. Below the method we added called test. Go ahead and add another public method under that. Name it whatever you want.

14
15
16
public function action_my_name($name){
		$this->request->response = 'Hello, my name is '.$name.'.';
	}

I named my action my_name. Note: that you must prefix action_ to the name of your methods. I also gave my method a parameter called $name. Now lets test it. Navigate to http://localhost/kohana/index.php/my_name/Zack. It hopefully outputted Hello, my name is Zack.

What this is doing is calling our welcome controller, and calling the my_name action, and passing that action a value, which could be anything for right now.

Conclusion

Thats it for today on learning what a controller is. Next time We will make a more advanced controller and do some real work with some Models. I hope this clarifies any questions you have. If you still are stuck on anything let me know in the comments. Thanks!

  • Corey

    Nice beginning. Just my luck that when I want to try to learn about Kohana that you have just started this series. Keep it simple for me man; I am new to Kohana and MVC (and PHP really, but a language is a language, for the most part).

    On a side note. I am searching for beginning tutorials and it seems that the http://learn.kohanaphp.com/ is no longer a viable place for tutorials. Do you have any suggestions?

  • CoreyHunt
  • http://zackperdue.com Zack Perdue

    Hello CoreyHunt, im glad you find my post helpful, i will be sure to continue the series. Im hoping to release a new tutorial once every 2 days or so. Thanks for reading!

  • http://www.google.com/profiles/Zetastate Corey

    Hey, I was just going over this again for reference and I realized there is a typo above:

    /localhost/index.php/kohana/my_name/Zack

    Unless, I am missing something, it should be:

    /localhost/kohana/welcome/my_name/Zack

    When is the next post coming out?

  • http://zackperdue.com Zack Perdue

    Thanks Corey for that! Its fixed now.

    About the next post – I’ve been really busy with websites lately. I’ll get another post up before Monday. Anything you particularly would request that I teach?

    Thanks,
    Zack

  • http://www.google.com/profiles/Zetastate Corey

    As a matter of fact, I do have a request. I am trying to build a site that has a members area and the level of those signed in determines what content is shown.

    Could you show how to make a simple CMS for site admins?

  • Zack

    Thanks Corey for that! Its fixed now.

    About the next post – I've been really busy with websites lately. I'll get another post up before Monday. Anything you particularly would request that I teach?

    Thanks,
    Zack