How to call a function for all controllers and actions

Hi,

I want to check user status in all actions If status=0, i want to redirect it for activation page.

Is there a way to run this function auto for all actions?




$userStatus = Yii::$app->session->get('user.status');

    if($userStatus['user_status'] == 0){

        return $this->redirect(['/profile/emailactivation']);

    }



I tried to add this into layout but its get error for redirect.

Calling unknown method: yii\web\View::redirect()

Thanks…

creating filters

$this refers to the view page ie yii\web\View.

yii\web\View Does not have a function called redirect thus throwing an error. I assumed you copied it from the controller. The controller base class has an function called redirect that’s why it works there.

You should check the user session variable in the controller before you render the page and just redirect/render the right page from the get go instead of potentially rendering a page just to redirect and using uneeccsary resources.

If you need help post your controller action that renders the page you are putting the redirect on.