Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of preventing rules and restrictions for tourists and users in Yii2

Shulou Source: shulou.com Published: 2022-06-03 13:02:56 10月04日 Update

The purpose of this article is to share with you how to solve the rules and restrictions of Yii2 against tourists and users. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Recently, I am working on a project with Yii2.0, which needs to implement a function: you can't access some pages without login, that is, visitor access restrictions. After searching the information for a long time, I finally found the answer. The solution is as follows:

In access, access means to visit, and there is a configuration item:

'only'= > [' login','about']

What does this mean? it means that it only works in the two action of login and about, that is, when action is login or about, it will enter the rules for further verification.

But what if we don't allow access to action except login and registration? There are other configurations. We change only to except. What does it mean? what does it mean except what? that is to say, it works on action except login and signup. Next, in:

The copy code is as follows:

Rules= > [['action'= > [' login','signup'], 'allow'= > true,'roles'= > ['?]

Rules is the rule, and multiple entries can be written in it. Actions refers to which action,allow the rule refers to whether access is allowed or not. The field 'roles'' is the key, which is the role of allowing access. Among them? On behalf of tourists, @ represents users who have logged in.

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'],],] } the above is how to solve the problem of Yii2's rules and restrictions against tourists and users. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Meaning tourists rules users restrictions that is login questions code representatives functions more knowledge articles this is part configuration practical next two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology Redmi Xiaomi OPPO Reno