Q:

yii2 redirect any page to login page after session timeout

i wanna ask about redirect any page to login page after session timeout in yii2, i try to do this
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => false,
            'returnUrl' => 'index.php?r=site/login',
            'identityCookie' => [
                'name' => '_backendIdentity',
            ],
        ],

        'session' => [
            'name' => 'BACKENDSESSID',
            'savePath' => __DIR__ . '/../runtime',
        ],

but it return to login page when i'm at index.php page, if i go to another action and the session timeout it will return 

PHP Notice – yii\base\ErrorException
  Trying to get property of non-object

1. in /var/www/html/spmoa/backend/controllers/StaffController.php at line 174
165166167168169170171172173174175176177178179180181182183

    public function actionMyProfile() {
       error here ->  $id = Yii::$app->user->identity->staff->id;
        $model = $this->findModel($id);

Please help me

A:

Try with behaviors
public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [

                [
                    'actions' => ['youraction'],
                    'allow' => true,
                    'roles' => ['@'], // you can use matchCallback to create  more powerful  check 
                ],
            ],
        ], 

    ];`enter code here`
}

