Kohana PHP 3.0 (KO3) Tutorial Part 5
Welcome to the fifth part in this series on how to develop with Kohana PHP V3 (KO3). If you haven’t read any of previous parts yet, I would search for and read them before going on. In this tutorial we will be going over the “H” in HMVC.
So most of you know what MVC is, but might not know about HMVC. HMVC extends MVC by allowing a controller to call on other controllers on do stuff, or doing stuff in an Hierarchical manor. Think modules that can work by themselves or in a groups. If that didn’t help, check out this article about the Hierarchical-Model-View-Controller pattern.
So we’ve been design pages with a single controller as a page, with some actions that pulls data in from a model and populates a parts of a view to give us a complete browser page. Well, that can get messy with all the model calls and extra snippets we bring into with views. Using HMVC, we can organize our code a lot better, make more things reusable and with the way Kohana PHP 3 does HMVC, we can even make our application scale by calling out to other servers to give us our content for a section.
Lets go ahead and make a new controller:
[php]<?php
defined(‘SYSPATH’) or die(‘No direct script access.’);
class Controller_Hmvc extends Controller_DefaultTemplate
{
public function action_index()
{
// Set meta data
$this->template->title = ‘Kohana 3.0 HMVC Test’;
$this->template->meta_keywords = ‘PHP, Kohana, KO3, Framework, HMVC’;
$this->template->meta_description = ‘A test of of the KO3 framework HMVC pattern’;
// Fill in content
$ko3 = array();
$ko3['posts'] = Request::factory(‘posts/getposts’)->execute()->response;
$this->template->content = View::factory(‘pages/hmvc’, $ko3);
}
}[/php]
Save this as “hmvc.php” in “application/classes/controller”. The above should look pretty familiar. One line should stand out:
[php]$ko3['posts'] = Request::factory(‘posts/getposts’)->execute()->response;[/php]
This line is where the HMVC magic happen. This will call the controller “posts” an invoke the action “getposts”, execute the code and get the response. We take the response and save it to the array which we fill the view with.
Talking about views, let make our view for this controller:
[php]<?php echo $posts;?>[/php]
Save this as “hmvc.php” in “application/views/pages/”
Now for the “posts” controller:
[php]<?php
defined(‘SYSPATH’) or die(‘No direct script access.’);
class Controller_Posts extends Controller
{
public function action_index()
{
}
public function action_getposts()
{
// Load model
$posts = new Model_Post();
// Fill content array for view with last 10 posts.
$content = array();
$content['posts'] = $posts->getLastTenPosts();
// Render and output.
$this->request->response = View::factory(‘pages/hmvc_posts’, $content);
}
}[/php]
Save this as “posts.php” in “application/classes/controller”. Once again, pretty straight forward. This controller extends the basic controller, so nifty template or anything and is pretty much what we did in the last tutorial.
Now for the view.
[php]<?php foreach($posts as $post):?>
<h1><?php echo $post['title'];?></h1>
<?php echo $post['post'];?>
<hr />
<?php endforeach;?>[/php]
Save this as “hmvc_posts.php” under “applications/views/pages”.
Now if you point your browser to: “http://yourserver/mykohana3/hmvc”, you should see a couple posts up on the screen.
We can achieve the same result by calling the controller from the view instead of the controller. This is helpful for keeping it simple. So one template used all over the place, you wouldn’t have to worry about having to putting in all the HMVC stuff into the controllers that used that template.
Change the “hmvc” controller to look like this:
[php]<?php
defined(‘SYSPATH’) or die(‘No direct script access.’);
class Controller_Hmvc extends Controller_DefaultTemplate
{
public function action_index()
{
// Set meta data
$this->template->title = ‘Kohana 3.0 HMVC Test’;
$this->template->meta_keywords = ‘PHP, Kohana, KO3, Framework, HMVC’;
$this->template->meta_description = ‘A test of of the KO3 framework HMVC pattern’;
// Fill in content
$ko3 = array();
$ko3['content'] = ‘Hello there!’;
$this->template->content = View::factory(‘pages/hmvc’, $ko3);
}
}[/php]
Now edit the “hmvc” view to look like this:
[php]<?php echo $content;?><br/>
<?php echo Request::factory(‘posts/getposts’)->execute()->response;?>[/php]
This is example is pretty simple and we could expand this to use parameters to deliver the data in different ways, say, simple html, full html, xml, json, etc..
Sources used: Unofficial Kohana 3 Wiki, iBuildings: Scaling Web Applications with HMVC





February 28, 2010 at 7:54 pm
March 1, 2010 at 9:35 am
March 1, 2010 at 8:39 pm
March 1, 2010 at 8:56 pm
March 3, 2010 at 7:32 pm
March 4, 2010 at 6:01 am
March 4, 2010 at 8:54 am
March 4, 2010 at 9:38 am
May 8, 2010 at 10:37 pm
May 10, 2010 at 7:43 am
May 14, 2010 at 11:52 am
May 14, 2010 at 11:59 am
May 18, 2010 at 12:26 pm
May 28, 2010 at 8:08 am
June 5, 2010 at 10:57 am
June 11, 2010 at 7:24 pm
June 12, 2010 at 8:15 am
June 13, 2010 at 7:21 am
June 23, 2010 at 9:57 pm
March 14, 2011 at 12:08 am
March 19, 2011 at 11:33 pm
March 19, 2011 at 11:37 pm
March 21, 2011 at 5:22 am
March 23, 2011 at 6:49 pm
March 24, 2011 at 5:01 am
April 16, 2011 at 2:54 pm
July 25, 2011 at 11:23 am
August 19, 2011 at 6:38 am
December 14, 2011 at 12:58 pm
March 14, 2012 at 2:56 am
March 14, 2012 at 3:54 am