In the definitive guide, there’s a tutorial on using gii. So I started looking through the generated code to understand the workflows. Here’s something that strikes as odd to me:
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->code]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
So when the user hits the Submit button and the model is updated, we are redirected to the Country’s view.php file, and the information we pass is “id”, which has the country code. So far so good, but now look at the “view” view:
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model app\models\Country */
$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Countries', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="country-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->code], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->code], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'code',
'name',
'population',
],
]) ?>
</div>
Where does $model come from, given that it wasn’t passed by the controller?