How to allow guest access to some actions in Yii2 Controller?

試著忘記壹切 提交于 2019-12-24 03:28:05

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/31407769/how-to-allow-guest-access-to-some-actions-in-yii2-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!