H
H
hollanditkzn2017-03-16 18:33:05
Yii
hollanditkzn, 2017-03-16 18:33:05

How to transfer user by session to another controller?

The question is that I do not fully understand how to transfer sessions. Writes an error Getting unknown property: yii\web\Request::session
The session does not work for me, I changed sessions many times in different controllers and nothing comes out for me.
There are 2 controllers after authorization the user enters the crud table, which is located on another controller. You can see the whole code at https://github.com/hollandit/crm.git

public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            $id_user = Yii::$app->session->get('id');
            $session = Yii::$app->request->session;
            $session ->set('id', $id_user);
            return $this->redirect(['/zakaz/index', 'id'=> $id_user]);
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

And in frontend/controllers/ZakazController
public function actionIndex()
    {
        $searchModel = new ZakazSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'id_user' => Yii::$app->session->get('id')
        ]);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2017-03-16
@slo_nik

Good evening.
And what for to you session for the authorized user?
All data of this user is stored in Yii::$app->user->identity

A
Alexey, 2017-03-17
@masterfreelance

I completely agree with slo_nik
And the code will become much better.
"Pants turn....."

public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) return $this->goHome();

        $model = new LoginForm();
        return ($model->load(Yii::$app->request->post()) && $model->login()) ? 
            $this->redirect(['/zakaz/index',]) :
            $this->render('login', ['model' => $model,]);
    }

public function actionIndex()
    {
        $searchModel = new ZakazSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'id_user' => (!Yii::$app->user->isGuest)?Yii::$app->user->identity->id:0;
        ]);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question