How can I perform checks on each page of application?

My application is allowing to authorize users who aren’t confirmed their e-mail address yet, but should inform them about it.

I found a solution, but I’m not sure it’s good enough, and I’m calling to Yii guru’s help!

After successfull authorization I’m saving user status:




public function login()

{

    if ($this->validate()) {

        $user = $this->getUser();

        Yii::$app->session->set('user.status', $user->status);

        return Yii::$app->user->login($user, $this->rememberMe ? 3600 * 24 * 30 : 0);

    } else {

        return false;

    }

}



After that I’m performing check in layout and rasing a flash message:




if (!Yii::$app->user->isGuest && Yii::$app->session->get('user.status') == \common\models\User::STATUS_PENDING) {

    Yii::$app->session->setFlash('warning', 'Confirm your e-mail address, please!');

}



Is it good or bad to perform such checks in layout and is there any other way to do it?

In Yii1 I’ve done such things by extending CController and making separate function in it.

Why you are using setFlash? Flash will be visible only in the next request. Why not to check status via:




if(!Yii::$app->user->isGuest && Yii::$app->user->identity == \common\models\User::STATUS_PENDING){


}



I’m not a Yii guru, but I like your idea. You just want to remind the user to verify. You could alternatively put it in the controller, but that will only clutter up controller logic, and what you are really looking for is a cosmetic effect, according to what you have stated so far. So I think your solution is effective in that case…

Thanks about your notice. I’ve rechecked my code, and really user->identity is working well. Don’t know why it hasn’t returned a value yesterday night. To be more correct this case should looks like:




if(!Yii::$app->user->isGuest && Yii::$app->user->identity->status === \common\models\User::STATUS_PENDING){


}



Anyway. The question mostly: How to do this checks not in the layout? I dont like to hold any logic and got dependencies with Models in them.

Thanks in advance!