This is a best practice question and not a specific issue. I'm fairly new to the MVC approach and Yii, and have developed on an app for a while now. I keep seeing talks on best practice and what to put in which file (controller, model, view, helper etc.) however i have not yet found anything specific in terms of examples.
I currently have calls like: Model::function() in my view files as well as checks like $var = app()->request->getParam(value, false);
I have calls in my controller file like Model::function() and Model::model()->scope1()->scope2()->findAll() I also think my controller files are getting a bit thick, but not sure how and where to put some of the bloat, i have been reading about the DRY and i think i'm not exactly DRY'ing my code so to speak. Could you give me a more clearer picture about what goes where, and suggestions or reasons why :) Appreciate any advice, thanks in advance.
here's an example call in a viewfile
<?php $this->pageTitle = 'Edit Action'; $this->subTitle = '<i>for</i> <b>' . Vendors::getName($_GET['vendor']) . '</b>'; ?> <div class="wrapper"> <?php echo $this->renderPartial('_form', array('model' => $model)); ?> </div> The getName is my function in the model, is this a good way to call a function in a view?
Another example view file:
<div class="wrapper"> <?php if($this->action->id != 'create') { $this->pageTitle = "New Media Contact"; echo $this->renderPartial('_form', array('model'=>$model)); } else { $this->pageTitle = "New Vendor"; echo $this->renderPartial('_form', array('model'=>$model)); } ?> </div> $model is set in the controller with type... Same question... could this be done.. cleaner..? better in terms of MVC and reusability/DRY?
EDIT After reading some of the responses here, esp. @Simone I refactored my code, and wanted to share what it looks like now...
public function actionCreate() { $model = new Vendors; // Get and Set request params $model->type = app()->request->getParam('type', Vendors::VENDOR_TYPE); $vendorsForm = app()->request->getPost('Vendors', false); // Uncomment the following line if AJAX validation is needed $this->performAjaxValidation($model); if ($vendorsForm) { $model->attributes = $vendorsForm; if ($model->save()) $this->redirect(array('/crm/vendors', array('type' => $model->type))); } $model->categories = Categories::getAllParents($model->type); $this->pageTitle = 'New ' . Lookup::item('VendorType', $model->type); $this->render('create', array( 'model' => $model, )); } and the view create.php
<div class="wrapper"> <?php echo $this->renderPartial('_form', array('model'=>$model));?> Thanks for all respnses
eval(), use of tight coupling, use of@for error suppression, deep inheritance hierarchies, not clear interfaces, computation in__construct()methods, use of static factory methods .. and so on