What is a Controller
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!
And if you haven't already, you should follow me on twitter!
Follow @zackperdue-
Corey
-
CoreyHunt
-
http://zackperdue.com Zack Perdue
-
http://www.google.com/profiles/Zetastate Corey
-
http://zackperdue.com Zack Perdue
-
http://www.google.com/profiles/Zetastate Corey
-
Zack