Yii HMVC

You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#3) »

  1. Introduction
  2. HMVC coding style
  3. Reusable HMVC controller
  4. hmvc views

Introduction

Right now we can use widgets + action provider like in this tutorial: How to use a Widget as an Action Provider

what i am going to introduce is a coding style that will result into a single page generated from different requests like in the example below this makes the specific request reusable instead of just an action being accessible to the controller in which the widgets are created

HMVC coding style

<div class="row-fluid">
    <div class="span6">
        <?php Yii::app()->runController('hmvc/default/customerList'); ?>
    </div>
    <div class="span6">
        <?php Yii::app()->runController('hmvc/default/quotationlist');?>
    </div>
</div>

i created an hmvc module which has this controller

Reusable HMVC controller

class DefaultController extends Controller {

    //standard cgridview code but uses renderpartial

    public function actionCustomerList(){
        $model = new Customer('search');
        $model->unsetAttributes();
        if(isset($_GET['Customer']))
            $model->attributes = $_GET['Customer'];
        $this->renderPartial('customer', array('model' => $model));
    }

    public function actionQuotationList() {
        $model = new Quotation('search');
        $model->unsetAttributes();
        if(isset($_GET['Quotation']))
            $model->attributes = $_GET['Quotation'];
        $this->renderPartial('quotation', array('model' => $model));
    }
    
    public function actionAnother(){
        echo "THIS IS FROM ANOTHER ACTION!";
    }


}

notice that i only used render partials or echo

hmvc views

customer.php

$this->widget('zii.widgets.grid.CGridView', array(
            'dataProvider' => $model->search(),
            'id'=>'customer-grid',
            'filter' => $model,
            'columns' => array('name'),
        ));

item_detail.php

$this->widget('bootstrap.widgets.TbGridView', array(
    'id' => 'item-detail-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        'id',
        'status'=>array(
            'name'=>'status',
            'filter'=>ItemDetail::itemAlias('status'),
            'value'=>'$this->filter[$data->status]',
        ),
    ),
   
));

By doing this we do not have make a reusable actions with CAction but instead we can use these requests all throughout the system.. just put in mind that the 'ids' being used should still be unique across the rendered final page. we also do not have to bother our controllers that are going to use our widgets, direct call from views are enough