1. <legend id='S2BKU'><style id='S2BKU'><dir id='S2BKU'><q id='S2BKU'></q></dir></style></legend>

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

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

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

        根据数据库结果设置 Yii2 catchAll 路由

        Set Yii2 catchAll route depending on database result(根据数据库结果设置 Yii2 catchAll 路由)

          • <bdo id='sC2yb'></bdo><ul id='sC2yb'></ul>
              <tfoot id='sC2yb'></tfoot>
              <legend id='sC2yb'><style id='sC2yb'><dir id='sC2yb'><q id='sC2yb'></q></dir></style></legend>
            1. <i id='sC2yb'><tr id='sC2yb'><dt id='sC2yb'><q id='sC2yb'><span id='sC2yb'><b id='sC2yb'><form id='sC2yb'><ins id='sC2yb'></ins><ul id='sC2yb'></ul><sub id='sC2yb'></sub></form><legend id='sC2yb'></legend><bdo id='sC2yb'><pre id='sC2yb'><center id='sC2yb'></center></pre></bdo></b><th id='sC2yb'></th></span></q></dt></tr></i><div id='sC2yb'><tfoot id='sC2yb'></tfoot><dl id='sC2yb'><fieldset id='sC2yb'></fieldset></dl></div>
            2. <small id='sC2yb'></small><noframes id='sC2yb'>

                  <tbody id='sC2yb'></tbody>

                  本文介绍了根据数据库结果设置 Yii2 catchAll 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  <?php
                  
                  namespace appmodulessitecontrollers;
                  
                  use Yii;
                  use yiifiltersAccessControl;
                  use yiiwebController;
                  use appmodelsSiteSettings;
                  
                  class CommonController extends Controller {
                  
                  public function init() {
                      Yii::$app->language = 'bg-BG';
                      Yii::$app->formatter->locale = 'bg-BG';
                      Yii::$app->params['siteSettings'] = SiteSettings::find()->one();
                  
                      if (Yii::$app->params['siteSettings']->in_maintenance == 1) {
                          Yii:$app->catchAll = ['index/maintenance', 'message' => Yii::$app->params['siteSettings']->maintenance_message];
                      }
                  }
                  

                  }

                  我试图从 CommonController init 方法中设置 catchAll 路由,但它抛出了一个错误:

                  I tried to set the catchAll route from within the CommonController init method, but it throws me an error:

                  从空值创建默认对象

                  是否可以根据数据库提供的条件设置 catchAll 路由?

                  Is it possible to set the catchAll route on condition provided from the database?

                  推荐答案

                  您需要在处理请求之前设置 catchAll 属性.init 方法是在重新设置控制器后执行的,所以不会有任何影响.您需要使用应用程序 onBeforeRequest 事件来设置 catchAll 路由.

                  You need to setup catchAll property just before request is handled. Init method is executed after resloving controller, so it won't have any effect. You need to use application onBeforeRequest event to setup up catchAll route.

                  在配置文件中设置如下:

                  In config file set following:

                  $config = [
                      'id' => '...',
                       ......
                  
                      'on beforeRequest' => function () {
                          Yii::$app->params['siteSettings'] = SiteSettings::find()->one();            
                          if (Yii::$app->params['siteSettings']->in_maintenance == 1) {
                              Yii::$app->catchAll = [
                                'index/maintenance', 
                                'message' => Yii::$app->params['siteSettings']->maintenance_message
                              ];
                          }
                      },
                      ....
                      'comonents' = [
                          ....
                      ]
                  ];
                  

                  您可以通过缓存 SiteSettings::find()->one() 对此进行一些改进;避免为每个请求打开与数据库的连接.

                  You can add little improvement to this by caching SiteSettings::find()->one(); to avoid opening connection to database for every request.

                  更新:我不确定 catchAll 是否可以用于特定模块,但您可以处理 onBeforeAction 事件并重定向到自定义路由.

                  Update: I am not sure if catchAll can be used for specific module, but you can handle onBeforeAction event and redirect to custom route.

                  'on beforeAction' => function ($event) {
                  
                      $actionId = $event->action->id;
                      $controllerId = $event->action->controller->id;
                      $moduleId = $event->action->controller->module->id;
                  
                      //TODO: Check module here
                      if (!(($controllerId == "site") && ($actionId == "offline")))
                      {
                          return Yii::$app->response->redirect(['site/offline']);
                      }
                  },
                  

                  这篇关于根据数据库结果设置 Yii2 catchAll 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的问题)

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

                        <bdo id='CPJOL'></bdo><ul id='CPJOL'></ul>

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

                        • <tfoot id='CPJOL'></tfoot>

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