Yii2:页面加载时出错'$model not defined'



我已经开发了表单,允许所有者创建团队。 代码是:

<?php $form = ActiveForm::begin(['id' => 'team-create-form', 'action' => ['site/create-team-form'], 'options' => array('role' => 'form')]);
<div class="col-lg-10 form-group" id="createTeamForm" style="margin-top: 15px;">
<div class="col-lg-4">
<?= $form->field($model, 'team_name',['template' => "{label}t{input}n{error}"])->textInput(array('placeholder'=>'Enter team name....')); ?>
 </div>
 <div class="col-lg-4">
  <?= $form->field($model, 'team_description',['template' => "{label}t{input}n{error}"])->textInput(array('placeholder'=>'Enter team Description....')); ?>
</div>
 <div class="col-lg-2">
<?= Html::submitButton('Submit', ['class' => 'btn btn-danger', 'id' => 'tsubmit', 'style' =>       'margin-top: 22.5px; margin-right: 15px;']) ?>
 </div>
</div>

尝试使用上述代码加载页面,但它向我显示错误"未定义$model"。如何解决这个问题。我需要在主本地添加一些东西吗.php???

public function actionLogin()
{
    $model = new LoginForm();
    $session = Yii::$app->session;
    if ($model->load(Yii::$app->request->post()) && $model->login()) {
        $collection1 = Yii::$app->mongodb->getCollection('users');
        $teamid = $collection1->findOne(array('username' => $model->email_address));
        $session->set('id', $teamid['_id']);
        $session->set('name', $teamid['name']);
        $session->set('username', $model->email_address);
        $collection2 = Yii::$app->mongodb->getCollection('teamdashboard');
        $teams = $collection2->find(array('admin' => $model->email_address));
        $model1 = new TeamCreateForm();
        return $this->render('dashboard', ['model'=>$model1, 'teams'=> $teams]);
    } elseif($session->isActive){
        $username = $session->get('username');
        $collection = Yii::$app->mongodb->getCollection('users');
        $teams = $collection->findOne(array('username' => $username));
        return $this->render('dashboard', ['teams'=>$teams]);
    }else{
        $this->layout = 'index';
        return $this->render('login', ['model' => $model]);
    }
}

我已将产品页面重命名为仪表板,以便更好地理解。

现在,当我运行它并登录时,地址栏网址显示url:..../web/index.php?r=site/login,而它应该显示url:..../web/index.php?r=site/dashboard,并向我显示仪表板的视图。

当我刷新页面时,我带回登录...

您是否在仪表板视图中使用了$model?如果你这样做 - 你需要通过它(与登录的方式相同)。

您必须将$model发送到视图。仅当您将视图发送给视图时,该视图才知道变量。

我不知道你对地址栏是什么意思。地址栏与您发送到视图的内容无关。

编辑
你的整个思维方式都很奇怪。为什么你会根据该人是否注册而显示不同的视图?

返回 $this->render('dashboard', ['teams'=>$teams]);返回 $this->render('login', ['model' => $model]);

用户使用参数重定向以将客户移动到另一个页面。拥有像/login 这样实际显示仪表板的 URL 是不合逻辑的。

最新更新