Yii2 require all Controller and Action to login(Yii2 需要所有 Controller 和 Action 登录)
问题描述
在我的站点控制器中我是这样写的
'访问' =>['类' =>访问控制::类名(),'规则' =>[['动作' =>['登录错误'],'允许' =>真的,],['动作' =>['logout', 'index' ,'call-back'],//添加所有动作以让访客进入登录页面'允许' =>真的,'角色' =>['@'],],],],所以如果我转到索引或回调操作,我将重定向到登录页面.但我必须为每个控制器的所有操作都这样做.你能告诉我最好的方法吗?
解决方案 将此规则放在 rules
部分的开头:
<预><代码>['允许' =>真的,'角色' =>['@'],],
省略 actions
表示所有操作.
所以你的 AccessControl
配置将是这样的:
公共函数行为(){返回 ['访问' =>['类' =>访问控制::类名(),'规则' =>[['允许' =>真的,'角色' =>['@'],],//...],],];}
请记住,规则是按照声明的顺序应用的.
要在没有继承的情况下全局执行此操作,请在应用程序配置中的 components
声明下方(不在内部!)添加 as beforeRequest
数组:
'components' =>[...],'如前请求' =>['类' =>'yii过滤器访问控制','规则' =>[['允许' =>真的,'动作' =>['登录'],],['允许' =>真的,'角色' =>['@'],],],'denyCallback' =>功能 () {return Yii::$app->response->redirect(['site/login']);},],
此代码将在每个请求之前运行,并阻止访客的除 login
之外的所有操作.
确保在 SiteController
之外的其他控制器中没有 login
操作.如果有(例如它们用于不同目的),请在相应的控制器中明确阻止它们.但这种情况非常罕见.
In my sitecontroller I write like this
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['logout', 'index' ,'call-back'], // add all actions to take guest to login page
'allow' => true,
'roles' => ['@'],
],
],
],
so If I go to index or call-back action,I'll redirected to login page. but I have to do it for all action to each controller. Could you tell me the best way to do it?
Place this rule in the beginning of the rules
section:
[
'allow' => true,
'roles' => ['@'],
],
Omitting the actions
means all actions.
So your AccessControl
config will be like this:
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
// ...
],
],
];
}
Keep in mind that rules are applied in order they are declared.
To do it globally without inheritance, add the as beforeRequest
array below (not inside!) the components
declaration in your application config:
'components' => [ ... ],
'as beforeRequest' => [
'class' => 'yiifiltersAccessControl',
'rules' => [
[
'allow' => true,
'actions' => ['login'],
],
[
'allow' => true,
'roles' => ['@'],
],
],
'denyCallback' => function () {
return Yii::$app->response->redirect(['site/login']);
},
],
This code will run before each request and block all actions except login
for guests.
Make sure that there is no login
action in other controllers than SiteController
. If there are (and for example they are for different purposes), block them explicitly in according controllers. But it's pretty rare case.
这篇关于Yii2 需要所有 Controller 和 Action 登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Yii2 需要所有 Controller 和 Action 登录
基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01