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

Set Yii2 catchAll route depending on database result(根据数据库结果设置 Yii2 catchAll 路由)
本文介绍了根据数据库结果设置 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 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)