如何允许访客访问 Yii2 控制器中的某些操作? [英] How to allow guest access to some actions in Yii2 Controller?
问题描述
我想知道如何配置我的控制器以允许以访客身份执行某些操作并能够在 Yii2 中显示该访客的视图.
我已经在我的行为中尝试过这个规则
'访问' =>['类' =>访问控制::类名(),'规则' =>[['动作' =>['创建','更新'],'允许' =>真的,'ips' =>['127.0.0.1'],]],]
这是我试过的配置:
'访问' =>['类' =>访问控制::类名(),'规则' =>[['允许' =>真的,'动作' =>['创建','更新'],'角色' =>['?'],],],]
Edit2:从头开始检查一个新项目(yii2-advanced)并尝试再次生成控制器和模型后,以前的规则起作用了,我认为是上一个项目的一些配置阻止了我作为以某种方式来客.
在你的规则中
<预><代码> ['允许' =>真的,'动作' =>['登陆注册'],'角色' =>['?'],],然后将允许匿名用户执行 login
和 signup
操作.
或者也可以使用only
方法,免除不需要acl
的动作.
更多详情,请参见http://www.yiiframework.com/doc-2.0/guide-security-authorization.html
I'd like to know how to configure my controller to allow some actions to be executed as a guest and be able to show the view for that guest in Yii2.
I've tried this rule in my behaviour
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['create','update'],
'allow' => true,
'ips' => ['127.0.0.1'],
]
],
]
Edit: This is the config I tried :
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['create', 'update'],
'roles' => ['?'],
],
],
]
Edit2: After checking out a new project(yii2-advanced) from scratch and trying to generate the controllers and models again the previous rules worked, I think it was some configuration from the previous project that were preventing me to access as a guest somehow.
In your rules
[
'allow' => true,
'actions' => ['login', 'signup'],
'roles' => ['?'],
],
Then the actions login
and signup
will be allowed for anonymous users.
Or you can use the only
method also, to exempt the action which does not require acl
.
For more details,see http://www.yiiframework.com/doc-2.0/guide-security-authorization.html
这篇关于如何允许访客访问 Yii2 控制器中的某些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!