Yii2动态维护模式

Abh*_*ran 0 php maintenance-mode yii2

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

我已经设置并测试了它,效果很好.但现在我想让它变得动态,如果管理员点击切换开关,网站应该进入维护模式.要实现它,我需要做的是使启用变量为true,这在此库的Maintenance类中使用.

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

提前致谢!

lub*_*sdz 5

将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' => [
    ...
]
Run Code Online (Sandbox Code Playgroud)

并在SiteController中创建操作"actionMaintenance":

public function actionMaintenance()
{
    return $this->render('maintenance');
}
Run Code Online (Sandbox Code Playgroud)

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

<h1>The site is currently under maintenance</h1>
<p>We apologize for inconvenience. Please come back later.</p>
Run Code Online (Sandbox Code Playgroud)

另见相关帖子.