• <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 登录

        Yii2 require all Controller and Action to login(Yii2 需要所有 Controller 和 Action 登录)
      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 登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                      <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'>

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