Yii2 analysis of solutions to visitor and user prevention rules and restrictions

不言
Release: 2023-03-25 07:12:02
Original
1283 people have browsed it

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']
Copy after login


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'=>['?']]]
Copy after login
, rules are the rules, you can write multiple ones here, actions refer to the rules for Which action, allow refers to whether access is allowed, the 'roles' field is key, this is the role that allows access.

in? Represents the visitor, @ represents the logged in user .

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'],
   ],
  ],
 ];
}
Copy after login

Related recommendations:

yii2 implements paging, an example of paging function with search

Yii2 Forgot password operation based on email verification

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template