search
HomeBackend DevelopmentPHP TutorialHow to solve Yii2 prevention rules and restrictions for visitors and users

This article mainly introduces Yii2’s solutions to tourists and user prevention rules and restrictions. It briefly analyzes the principles and corresponding setting methods of Yii2’s tourists 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? , which means that 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, in:

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

, rules are rules. You can write multiple ones here. actions refers to which action the rules are for. allow refers to whether access is allowed. The 'roles' field is key. This is Roles allowed 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'],
   ],
  ],
 ];
}

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website !

Related recommendations:

How to deal with the escape of routing links in Yii2.0 Basic code

About combining in yii2 Code for gridview using modal pop-up window

How to create simple widgets using yii2.0

The above is the detailed content of How to solve Yii2 prevention rules and restrictions for visitors and users. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Windows 11 上缺少本地用户和组:如何添加它Windows 11 上缺少本地用户和组:如何添加它Sep 22, 2023 am 08:41 AM

“本地用户和组”实用程序内置于“计算机管理”中,可以从控制台访问,也可以独立访问。但是,一些用户发现Windows11中缺少本地用户和组。对于可以访问它的一些人来说,该消息显示,此管理单元可能不适用于此版本的Windows10。若要管理此计算机的用户帐户,请使用“控制面板”中的“用户帐户”工具。该问题已在上一次迭代Windows10中报告,并且通常是由于用户端的问题或疏忽引起的。为什么Windows11中缺少本地用户和组?您运行的是Windows家庭版,本地用户和组在专业版及更高版本上可用。活动

探索Windows 11指南:如何访问旧硬盘驱动器上的用户文件夹探索Windows 11指南:如何访问旧硬盘驱动器上的用户文件夹Sep 27, 2023 am 10:17 AM

由于权限,并不总是可以访问某些文件夹,在今天的指南中,我们将向您展示如何在Windows11上的旧硬盘驱动器上访问用户文件夹。此过程很简单,但可能需要一段时间,有时甚至数小时,具体取决于驱动器的大小,因此请格外耐心并严格按照本指南中的说明进行操作。为什么我无法访问旧硬盘上的用户文件夹?用户文件夹的所有权属于另一台电脑,因此您无法对其进行修改。除了所有权之外,您对该文件夹没有任何权限。如何打开旧硬盘上的用户文件?1.取得文件夹的所有权并更改权限找到旧的用户目录,右键单击它,然后选择属性。导航到“安

Windows 11 KB5031455无法安装,导致某些用户出现其他问题Windows 11 KB5031455无法安装,导致某些用户出现其他问题Nov 01, 2023 am 08:17 AM

Microsoft开始推出作为Windows503145511H22或更高版本的可选更新向公众KB2。这是第一个默认启用Windows11Moment4功能的更新,包括受支持区域中的WindowsCopilot、对“开始”菜单中项目的预览支持、任务栏的取消分组等。此外,它还修复了Windows11的几个错误,包括导致内存泄漏的潜在性能问题。但具有讽刺意味的是,2023年<>月的可选更新对于尝试安装更新的用户甚至已经安装更新的用户来说都是一场灾难。许多用户不会安装此Wi

Win11 新版画图:一键移除背景实现抠图功能Win11 新版画图:一键移除背景实现抠图功能Sep 15, 2023 pm 10:53 PM

微软邀请Canary和Dev频道的WindowsInsider项目成员,测试和体验新版画图(Paint)应用,最新版本号为11.2306.30.0。本次版本更新最值得关注的新功能是一键抠图功能,用户只需要点击一下,就能自动消除背景,凸显画面主体,便于用户后续操作。整个步骤非常简单,用户在新版画图应用中导入图片,然后点击工具栏上“移除背景”(removebackground)按钮,就可以删除图片中的背景,用户也可以使用矩形来选择要消除背景的区域。

TranslucentTB不起作用:如何解决TranslucentTB不起作用:如何解决Jun 06, 2023 am 08:21 AM

TranslucentTB是寻求时尚简约桌面外观的Windows11爱好者广泛使用的工具,遇到了障碍。自从发布以来Windows11内部版本22621.1344(22H2)28年2023月日,TranslucentTB对大多数用户不起作用。此错误使用户努力应对其任务栏的有限自定义选项。用户在寻求克服这一挫折的解决方案时,挫败感显而易见。在最近的Windows11更新之后,TranslucentTB无法正常工作的问题已在多个在线平台上广泛报道,包括论坛和社交媒体。用户一直在分享他们的经验,拼命寻找

mysql 怎么修改hostmysql 怎么修改hostFeb 15, 2023 am 10:17 AM

mysql修改host的方法:1、通过“sudo service mysql stop”停掉mysql服务;2、以安全模式启动mysql,然后重置root密码;3、通过“update user set Host='%' where User='hive';”语句修改host即可。

如何下载iPhone地图以供离线使用如何下载iPhone地图以供离线使用Nov 04, 2023 pm 11:13 PM

随着iOS17的推出,Apple使iPhone用户可以下载地图以供离线使用。此功能为可能前往互联网连接不可靠或不存在的地区的用户提供了实用的解决方案。通过将地图下载到他们的设备上,用户可以确保即使失去蜂窝连接,他们也可以始终访问他们想要的路线。离线地图不仅仅是一个位置的静态图像。它们为用户提供了丰富的信息,例如地点的小时数和评级、转弯路线和预计到达时间。这意味着即使没有Wi-Fi或蜂窝连接,用户仍然可以导航和访问有关周围环境的关键信息。在“地图”中使用搜索功能打开“地图”应用,在搜索中搜索某个地

yii2 怎么去掉jqueryyii2 怎么去掉jqueryFeb 17, 2023 am 09:55 AM

yii2去掉jquery的方法:1、编辑AppAsset.php文件,注释掉变量$depends里的“yii\web\YiiAsset”值;2、编辑main.php文件,在字段“components”下面添加配置为“'yii\web\JqueryAsset' => ['js' => [],'sourcePath' => null,],”即可去掉jquery脚本。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools