(H)MVC for Hierarchical

HMVC (Hierarchical model–view–controller) Requests


WordPress Fuel includes a flexible HMVC request system. HMVC promotes reusability by separating common tasks. WordPress Fuel has internal request dispatching mechanism. Using HMVC, you can call many requests inside each other. HMVC pattern offers more flexibility in your application

        ------  Dashboard
        | C  | -------
        ------        \
       /      \        \
------        -------   \
| M  |        |  V  |    \
------        -------   ------ Posts
                        | C  | ------   
                        ------        \
                       /      \        \
                ------        -------   \
                | M  |        |  V  |    \
                ------        -------   ------ Comments
                                        | C  |          
                                        ------
                                       /      \
                                ------        -------
                                | M  |        |  V  |
                                ------        ------- 

HMVC Example - Portfolio Item & Comments

//wp-content/plugins/galleryplugin/mvc/controls/portfolio/item.php
class galleryplugin_portfolio_item_controller extends absController
{	
    public function index()
    {
        $item_id = $this->_request->getParam('item_id',0);
        if($item_id > 0)
        {
            $comments = $this->forward('item_comments/get',array(
                'item_id' => $item_id
            )); //clsHTTP_Response object
            
            $this->ViewData('comments',$comments)
                ->View('portfolio/item/page'); 
        else
        {
            //shot item listing page
            $this->View('portfolio/item/listing');
        }
    }
}

  • Line 13: Uses HMVC to get the list of comments by passing item_id
  • It is the responsibility of item_comments controller to get the list of comments pertaining to item and handles pagination too.


Item Comments Controller

//wp-content/plugins/galleryplugin/mvc/controls/portfolio/item/comments.php
class galleryplugin_portfolio_item_commens_controller extends absController
{	
    public function before()
    {
        if($this->is_hmvc_request() == false)
        {
            //blocks the direct access
            die('Silence is golden');
        }
    }
    
    public function action_get()
    {
        $item_id = (int)$this->get_hmvc_params('item_id');

        $oModelComments = new modItem_Comments(); 
        $comments = $oModelComments->getList($item_id);

        $this->ViewData('comments', $comments)
                ->View('portfolio/item/comments/list');
  
    }
}

Extending with absController_Widget

//wp-content/plugins/galleryplugin/mvc/controls/portfolio/item/comments.php
class galleryplugin_portfolio_item_commens_controller extends absController_Widget
{	
    public function action_get()
    {
        ...
    }
}

Noticeable that the before() method is not present. The hmvc_request check is already present inside absController_Widget. By extending sub controllers with absController_Widget ensures that you don't miss to block the external request to the sub controller.


Accessing Initial Request

//wp-content/plugins/galleryplugin/mvc/controls/portfolio/item/comments.php
class galleryplugin_portfolio_item_commens_controller extends absController_Widget
{	
    public function action_index()
    {
        //Initial controller name
        $initial_request = $this->_initital_request;

        //will return 'portfolio_item'
        $request_controller = $initial_request->getController(); 

        //will return 'index'
        $request_action = $initial_request->getAction(); 
    }
}

Noticeable that the before method is not present. The hmvc_request check is already present inside absController_Widget. By extending sub controllers with absController_Widget ensures that you don't miss to block the external request to the sub controller.