Yii 2 redirecting to Login if Guest visit the website

前端 未结 3 1674
不知归路
不知归路 2021-01-21 19:31

I need some advice how to make redirecting to login if someone does not login into the website and he is only Guest

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-21 20:22

    use Yii;
    use \yii\helpers\Url;
    
    if ( Yii::$app->user->isGuest )
        return Yii::$app->getResponse()->redirect(array(Url::to(['site/login'],302)));
    

    Use can use it in actions or views , but if you need to use it in lots of actions you probably need look at AccessControl and restrict access even before action is fired

提交回复
热议问题