1
votes

I have developed form to allow owner to create team. code is:

<?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>

I have tried loading the page with the above code but it is showing me error "$model not defined". How to resolve that. Am i need to add something in the main-local.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]);
    }
}

I have renamed the productpage as dashboard for better understanding.

Now when i run this & logs in, The address bar url shows url:..../web/index.php?r=site/login whereas it should show me url:..../web/index.php?r=site/dashboard & shows me the view of dashboard.

When i refresh the page, i brings me back to the login...

2
are u sending the $model to view?? - Harish Kurup
i am not sending $model to views bcoz if i do so then the url on address bar remains same as that of before dashboard. that will cause problem. - Shaggie
i agree with @Mihai, please gothrough the documentations yiiframework.com/doc-2.0/guide-structure-views.html - Harish Kurup

2 Answers

0
votes

Did you use $model in dashboard view? If you do - you need to pass it (the same way as the login).

0
votes

You have to send the $model to the view. The view only knows variables if you send it to it.

I have no idea what you mean with the address bar. The address bar has nothing to do with what you send to the view.

EDIT
Your entire way of thinking is strange. Why would u show different views depending if the person is registered or not?

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

User redirect with parameters to move the customer to another page. Having an URL like /login that actually shows a dashboard is not logical.