Thanks I tried your given code as well. But nothing has changed for me.I think there is some misunderstanding about this problem. Kindly go through below steps.
user enter valid username/password on login screen.
return true after successful validation. It means that user identity has invoked.I am able to get $id = Yii::$app->user->id; as well as Yii::$app->user->identity->username.
From this point user has redirected to some xyz page.
Now user is clicking any other link from menu. At that time Yii::$app->user->identity is getting NULL.
So step 3) is having a problem. It means that Yii::$app->user->login($this->getUser(), $this->rememberMe ? 36002430 : 0) is not holding user identity even for a few minutes. I hope it will give you more insight of my problem.
So,finally I was able to figure it out that where was a mistake . Some where in one of custom widget view file there was line of code, which i found in between the HTML tag.And that piece of line was something like as given below.
Yii::$app->user->logout();
Yes it is …it was a source of problem, which was destroying a user session.And off course it has to do it as it’s natural behaviour.
But in the end I would like to thank you jacome,bizley and softark for your constant watch on this topic and for your inputs here . This kind of support make our community more stronger.
On the link below you can find a good tutorial explaining how to login user from a database in Yii2. The only difference between your case and tutorial is that your sessions are stored in DB and in tutorial sessions are stored in files, but you can easily change it to the database.