Yii HMVC

You are viewing revision #3 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#2)

  1. Introduction
  2. HMVC coding style
  3. Reusable HMVC controller
  4. HMVCviews

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>

Reusable HMVC controller

i created an hmvc module which has this 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!";
    }


}

HMVCviews

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