這是一個最佳實踐問題,而不是一個具體問題。 我對MVC方法和Yii相當陌生,現在已經開發了一段應用程序。我一直在看最好的實踐和什麼放在哪些文件(控制器,模型,視圖,助手等)的談話,但我還沒有找到任何具體的例子。mvc最佳實踐視圖/控制器/模型在php和yii
我現在有這樣的方法調用:在我看來,文件Model::function()
以及像$var = app()->request->getParam(value, false);
我有像我型控制器文件調用::()函數和型號::模型()檢查 - > scope1( ) - > scope2() - > findAll()我也認爲我的控制器文件有點厚,但不知道如何以及在哪裏放一些膨脹,我一直在閱讀關於DRY,我想我不是可以這麼說,正在幹我的代碼。 你能給我一個更清晰的圖片,說明爲什麼會發生什麼,以及建議或理由:) 感謝任何建議,提前致謝。
這裏是在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>
行getName是我在模型的功能,這是調用視圖中的一個功能的好方法?
又如視圖文件:
<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>
$模式設置類型爲控制器... 同樣的問題......這哪是做..清潔..?在MVC和可重用性/ DRY方面更好?
編輯 在閱讀了一些在這裏的回覆後, @Simone我重構我的代碼,並希望分享它是什麼樣子現在...
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,
));
}
和視圖create.php
<div class="wrapper">
<?php echo $this->renderPartial('_form', array('model'=>$model));?>
感謝所有respnses
正如你的問題所示,它可能會被關閉。也許發佈一個具體的例子呢? –
你已經在使用Yii ..它本身會拋出窗外的最佳做法 –
@tereško請解釋一下你的意思,yii不使用最佳做法?你會建議使用哪種框架?我一直在看Symfony,並喜歡文件的數量等,但yii已經很容易與大多數工作... – SeventySix