Yii 2 redirecting to Login if Guest visit the website

前端 未结 3 1675
不知归路
不知归路 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:21

    Use the access section to set access to various actions in the controller.

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'only' => ['create', 'update','index'],
                'rules' => [
                    // deny all POST requests
                    [
                        'allow' => false,
                        'verbs' => ['POST']
                    ],
                    // allow authenticated users
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    // everything else is denied
                ],
            ],
        ];
    }
    

提交回复
热议问题