沃梦达 / 编程问答 / php问题 / 正文

Yii2 需要所有 Controller 和 Action 登录

Yii2 require all Controller and Action to login(Yii2 需要所有 Controller 和 Action 登录)

本文介绍了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 登录

基础教程推荐