• <bdo id='Qdnul'></bdo><ul id='Qdnul'></ul>

      <small id='Qdnul'></small><noframes id='Qdnul'>

    1. <i id='Qdnul'><tr id='Qdnul'><dt id='Qdnul'><q id='Qdnul'><span id='Qdnul'><b id='Qdnul'><form id='Qdnul'><ins id='Qdnul'></ins><ul id='Qdnul'></ul><sub id='Qdnul'></sub></form><legend id='Qdnul'></legend><bdo id='Qdnul'><pre id='Qdnul'><center id='Qdnul'></center></pre></bdo></b><th id='Qdnul'></th></span></q></dt></tr></i><div id='Qdnul'><tfoot id='Qdnul'></tfoot><dl id='Qdnul'><fieldset id='Qdnul'></fieldset></dl></div>
      <tfoot id='Qdnul'></tfoot>

    2. <legend id='Qdnul'><style id='Qdnul'><dir id='Qdnul'><q id='Qdnul'></q></dir></style></legend>

      1. Yii2 需要所有 Controller 和 Action 登录

        时间:2023-08-18
      2. <tfoot id='93YLf'></tfoot>
          <tbody id='93YLf'></tbody>
        1. <small id='93YLf'></small><noframes id='93YLf'>

          <legend id='93YLf'><style id='93YLf'><dir id='93YLf'><q id='93YLf'></q></dir></style></legend>
              <bdo id='93YLf'></bdo><ul id='93YLf'></ul>

                1. <i id='93YLf'><tr id='93YLf'><dt id='93YLf'><q id='93YLf'><span id='93YLf'><b id='93YLf'><form id='93YLf'><ins id='93YLf'></ins><ul id='93YLf'></ul><sub id='93YLf'></sub></form><legend id='93YLf'></legend><bdo id='93YLf'><pre id='93YLf'><center id='93YLf'></center></pre></bdo></b><th id='93YLf'></th></span></q></dt></tr></i><div id='93YLf'><tfoot id='93YLf'></tfoot><dl id='93YLf'><fieldset id='93YLf'></fieldset></dl></div>
                  本文介绍了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 登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Yii 框架中包含 CSS、javascript 文件 下一篇:Yii2 全局过滤器/行为强制用户首先进行身份验证

                  相关文章

                    <bdo id='TwhhL'></bdo><ul id='TwhhL'></ul>
                  <tfoot id='TwhhL'></tfoot>
                  <i id='TwhhL'><tr id='TwhhL'><dt id='TwhhL'><q id='TwhhL'><span id='TwhhL'><b id='TwhhL'><form id='TwhhL'><ins id='TwhhL'></ins><ul id='TwhhL'></ul><sub id='TwhhL'></sub></form><legend id='TwhhL'></legend><bdo id='TwhhL'><pre id='TwhhL'><center id='TwhhL'></center></pre></bdo></b><th id='TwhhL'></th></span></q></dt></tr></i><div id='TwhhL'><tfoot id='TwhhL'></tfoot><dl id='TwhhL'><fieldset id='TwhhL'></fieldset></dl></div>

                  <small id='TwhhL'></small><noframes id='TwhhL'>

                    <legend id='TwhhL'><style id='TwhhL'><dir id='TwhhL'><q id='TwhhL'></q></dir></style></legend>