例如:
我们有不同类型的表(book
,Horror
,Travel
)。
我们需要为这些书籍创建一个History
方法,但是对于每种书籍类型,我们需要显示不同类型的表单。
我们如何才能做到这一点?
以下代码是否正确?
public function actionUpdate($id)
{
$model = $this->getType($id);
switch ($model->type){
case Book::TYPE_HORROR:
return $this->render('update_horror', [
'model' => $model
]);
break;
case Book::TYPE_TRAVEL:
return $this->render('update_travel', [
]);
break;
case Book::TYPE_HISTORY:
return $this->render('update_history', [
]);
}
throw new NotFoundHttpException;
}
最佳答案
你可以这样做
public function actionUpdate($id)
{
$model = $this->getType($id);
return $this->render('update_'. $model->type, ['model' => $model]);
}
其中视图名必须是“update”+exactly$model->键入result,
例如:horror=update\u orror.php
这是为了更新视图,但不要忘记将动作方法添加到窗体中,否则它将查找不存在的页面。
$form = ActiveForm::begin(['action' =>['book/update']])