📜 ⬆️ ⬇️

Introducing Kohana 3.0 - Part 5

Meet the fifth part of a series of articles on development with Kohana PHP V3 (KO3). Previous parts can be found under the tag " familiarity with kohana 3.0 ". In this part we will look at HMVC (Hierarchical-Model-View-Controller).

HMVC is a continuation of MVC (model-view-controller), allowing the controller to give specific instructions to other controllers in a hierarchical order. To understand this, imagine modules that can work on their own or in groups. If that doesn't work, check out this illustration of the Hierarchical-Model-View-Controller pattern .

Previously, we designed pages for which a single controller was responsible with several actions, receiving data from the model and properly composing views into one full-fledged web page. HMVC allows you to organize your code much better and make it more ready for reuse. And with the way HMVC is implemented in Kohana 3, you can even contact other servers for content for sections.

Let's create a new controller:
')
<?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); } } 


Save the file as “hmvc.php” in the “application / classes / controller” folder.

The code above should look familiar, although one line stands out:

 $ko3['posts'] = Request::factory('posts/getposts')->execute()->response; 


This is where the HMVC magic happens. This is where the “getposts” action is called from the “posts” controller, which returns the result that we get and save to an array, which is passed on to the next view.

And since we started on views, let's create one for this controller:

 <?php echo $posts;?> 


Save it as “hmvc.php” in the “application / views / pages /” folder.

Now, actually, let's make the “posts” controller:

 <?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); } } 


Save the file as “posts.php” in the “application / classes / controller” directory.

Now open a clean document for a new look:

 <?php foreach($posts as $post):?> <h1><?php echo $post['title'];?></h1> <?php echo $post['post'];?> <hr /> <?php endforeach;?> 


Save it as “hmvc_posts.php” in the “applications / views / pages” folder.

If you now open “http: // localhost / mykohana3 / hmvc” in your browser, several entries should appear on the screen. The same result can be achieved by referring to the controller directly from the view. Since we use one template, we will not need to implement the HMVC in all controllers using this template.

Bring the file “hmvc.php” from the folder “application / classes / controller” to the following form:

 <?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); } } 


Now edit “hmvc.php” from the “application / views / pages /” folder to make it look like this:

 <?php echo $content;?><br/> <?php echo Request::factory('posts/getposts')->execute()->response;?> 


This is a fairly simple example that can be extended by making it possible to send data in different formats, for example: simple html, html, full html, xml, json, and so on.

Source: https://habr.com/ru/post/111959/


All Articles