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 路由

        时间:2023-10-15

          • <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 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 yii2 中访问 post json 数据时出错 下一篇:无法在 Windows 7 中打开输入文件 composer.phar

                  相关文章

                  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'>

                    1. <tfoot id='CPJOL'></tfoot>

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