This article mainly introduces Yii2’s solutions to visitor and user prevention rules and restrictions. It briefly analyzes the principles and corresponding setting methods of Yii2’s visitor and user prevention rules and restrictions. Friends in need can refer to the following
This article analyzes Yii2’s solutions to visitor and user prevention rules and restrictions with examples. I would like to share it with you for your reference. The details are as follows:
I am currently working on a project using Yii2.0, and I need to implement a function: some pages cannot be accessed without logging in, that is, visitor status access restrictions. After checking the information for a long time, I finally found the answer. The solution is as follows:
In access, access means access, and there is a configuration item:
'only'=>['login','about']
What does this mean? It only works within the two actions of login and about, that is, when the action is login or about, it will enter the rules for the next step of verification.
But what if we want to deny access to other actions except login and registration? There are other configurations. We change only to except. What does it mean? It means to exclude something, that is, it works for actions other than login and signup. Next, copy the code in:
## The code is as follows: rules=>[['action'=>['login','signup'],'allow'=>true,'roles'=>['?']]]
in? Represents the visitor, @ represents the logged in user . Related recommendations: The above is the detailed content of Yii2 analysis of solutions to visitor and user prevention rules and restrictions. For more information, please follow other related articles on the PHP Chinese website!public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'except' =>['login','signup'],
'rules' => [
[
'actions' => ['login','signup'],
'allow' => true,
'roles' => ['?'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}