Yii HMVC

10 followers

This wiki article has not been tagged with a corresponding Yii version yet.
Help us improve the wiki by updating the version information.

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

Total 6 comments

#14318 report it
ezekielnoob at 2013/08/04 01:39pm
Re: ajax

yes only the initial ajax script loaded are the ones needed, i have my tests actually... i still have to make a project page to show it though. thanks

#14310 report it
phreak at 2013/08/04 08:58am
ajax

actually the grid needs ajax for sorting and paging.

#14299 report it
ezekielnoob at 2013/08/02 10:24pm
Thanks

thank you for the replies. this is just an overview on how hmvc works i found some extensions that could help us make the implementation cleaner ehttpclient runactions

#14295 report it
Cherif at 2013/08/02 11:42am
SRP

IMO this design breaks the SRP (Single Responsability Principal) and is not reusable any more, HMVC is not the answer for every situation but have separated actions is the only good design I can go with for now.

#14294 report it
ezekielnoob at 2013/08/02 11:37am
js wont generate e.g. grid and other stuff wont work

@phreak actually CGridView doesn't need processOutput to work atleast to my example. processOutput is only required if you want an ajax loaded content to register its scripts. in which in the case of CGV is not required

#14290 report it
phreak at 2013/08/02 05:08am
js wont generate e.g. grid and other stuff wont work

If you want the JS to be generated and appended to the document you should call renderPartial with two more params

renderPartial(view, params, false, true);

Leave a comment

Please to leave your comment.

Write new article