I want to show data based on user's group id, which is i use
Yii::$app->user->identity->group_id
in my ModelSearch like this :
$query->andFilterWhere([
......
'group_id' => Yii::$app->user->identity->group_id,
'created_date' => $this->dreated_date,
]);
It works when i have session in the application, but when i don't have session the page show error :
Trying to get property of non-object
For 'group_id' => Yii::$app->user->identity->group_id,
How to make the page redirect to login page when i open that page if I don't have any session. Thank You.
EDIT
I added
if (Yii::$app->user->isGuest) {
return $this->redirect(Yii::$app->user->loginUrl);
}
in every action in my contoller that access ModelSearch .
You can add access control filter for this action.
Add in the controller:
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['ID of your action here'],
'rules' => [
// allow authenticated users
[
'allow' => true,
'roles' => ['@'],
],
],
],
];
}
Or you can add the redirect directly in the action - for example place it at the beginning:
if (Yii::$app->user->isGuest) {
return $this->redirect(Yii::$app->user->loginUrl);
}