问题描述
在我的站点控制器中我是这样写的
'访问' =>['类' =>访问控制::类名(),'规则' =>[['动作' =>['登录错误'],'允许' =>真的,],['动作' =>['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 登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)