1

我在 Yii2 项目中使用了以下库:单击此处

我已经对其进行了设置并对其进行了测试,并且效果很好。但是现在我想让它动态化,就像管理员点击切换开关一样,网站应该进入维护模式。为了实现它,我需要做的就是使该库的维护类中使用的启用变量为 true。

但我的问题是如何将我的切换开关链接到该变量。

提前致谢!

4

2 回答 2

7

将 Yii2 站点设置为维护模式意味着在处理请求之前强制路由。这可以通过配置on beforeRequest闭包简单地完成:

在 /config/web.php

return [
    ...
    'bootstrap' => ['log'],
    'on beforeRequest' => function ($event) {
        if (Yii::$app->params['portalMode'] == 'maintenance') {
            $letMeIn = Yii::$app->session['letMeIn'] || isset($_GET['letMeIn']);
            if (!$letMeIn) {
                Yii::$app->catchAll = [
                    // force route if portal in maintenance mode
                    'site/maintenance',
                ];
            }else{
                Yii::$app->session['letMeIn'] = 1;
            }
        }
    },
    'components' => [
    ...
]

并在 SiteController 创建动作“actionMaintenance”:

public function actionMaintenance()
{
    return $this->render('maintenance');
}

并在视图中views/site/maintenance.php调整您的布局:

<h1>The site is currently under maintenance</h1>
<p>We apologize for inconvenience. Please come back later.</p>

另见相关帖子

于 2015-08-27T20:58:20.060 回答
3

您可以访问应用程序的组件,如下所示:

Yii::$app->componentName

因此,您可以使用此组件访问它,如下所示:

Yii::$app->maintenanceMode->enable=FALSE;
于 2014-12-12T21:00:30.810 回答