Php 如何在视图中创建具有不同表单的页面(yii2)?
例如: 我们有不同类型的表(Php 如何在视图中创建具有不同表单的页面(yii2)?,php,yii2,Php,Yii2,例如: 我们有不同类型的表(恐怖,旅行,历史) 我们需要为这些书籍创建一个方法update,但是对于每种书籍类型,我们需要显示不同类型的表单 我们如何才能做到这一点 以下代码正确吗? public function actionUpdate($id) { $model = $this->getType($id); switch ($model->type){ case Book::TYPE_HORROR: return $th
恐怖
,旅行
,历史
)
我们需要为这些书籍创建一个方法update
,但是对于每种书籍类型,我们需要显示不同类型的表单
我们如何才能做到这一点
以下代码正确吗?
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]);
}
其中视图名称必须为“更新”+精确的$model->type result
例如:horror=update_orror.php
这是针对reneder视图的,但是不要忘记向表单中添加action方法,否则它将查找不存在的页面
$form = ActiveForm::begin(['action' =>['book/update']])