【问题标题】:How to build in 'maintenance mode' in Codeigniter?如何在 Codeigniter 中构建“维护模式”?
【发布时间】:2021-02-24 23:19:09
【问题描述】:

我正在使用最新的 codeigniter,当设置为“true”时,我需要创建一个标志(最好在配置中),所有页面都会显示“维护模式”消息,而不是执行它们的控制器代码。

这样做的最佳/最简单的做法是什么?

【问题讨论】:

  • 我个人认为,您最好制作一个模块,将维护模式状态保存在数据库中。因此,您可以在应用程序中动态地打开和关闭它。
  • @tereško 我是 mvc 新手,我希望确保我能够正确设置此类功能。

标签: php codeigniter


【解决方案1】:

这是我的解决方案,对我来说很好:

下面会立即调用 maintanance.php,这样你就可以在不被人看到的情况下继续破坏你的 CI 代码。

还允许您添加自己的 IP 地址,以便您仍然可以访问该站点进行测试等。

在 index.php 顶部添加:

$maintenance = false; ## set to true to enable

if ($maintenance)
{
    if (isset( $_SERVER['REMOTE_ADDR']) and $_SERVER['REMOTE_ADDR'] == 'your_ip')
    {
        ##do nothing
    } else
    {

        error_reporting(E_ALL);
        ini_set('display_errors', 1); ## to debug your maintenance view

        require_once 'maintenance.php'; ## call view
        return;
        exit();

    }
}

在与 index.php 相同的文件夹中添加文件 Maintanance.php(或更新上面的路径):

<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Maintenance</title>

        <style>
            body {
                width:500px;
                margin:0 auto;
                text-align: center;
                color:blue;
            }
        </style>
    </head>

    <body>

        <img src="images/home_page_logo.png">

        <h1><p>Sorry for the inconvenience while we are upgrading. </p>
            <p>Please revisit shortly</p>
        </h1>
        <div></div>

        <img src="images/under-maintenance.gif"   >

    </body>
</html>
<?php
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 3600');
?>

【讨论】:

  • 这个整体解决方案没有问题,但您不能在 HTML 之后 发送标题。标头必须是您发送给用户的第一件事。
  • 感谢卡门!这是我非常喜欢的解决方案,因为它可以在执行任何 CI 代码之前实现维护模式。 +1 更多用于允许开发者 IP 访问。
【解决方案2】:

通过在核心目录中放置一个名为 MY_Controller 的新文件来扩展 CI_Controller。

在此文件的构造函数中,执行以下操作:

public function __construct()
{
    parent::__construct();

    if($this->config->item('maintenance_mode') == TRUE) {
        $this->load->view('maintenance_view');
        die();
    }
}

让您应用中的所有控制器都继承自该类。

【讨论】:

  • 您的(安全)实时网站变得非常不安全:P。但这就是OP所要求的方式,所以+1 :)
  • @Allendar 你能解释一下这种变化是如何使网站变得不安全的吗?
  • 当每个请求在加载维护视图后终止时,人们如何'仍然访问[ing] it'?
  • 我已经尝试实现这一点,当维护模式设置为 TRUE 时,我得到一个空白页,尽管我的维护视图文件有内容。如果我删除“die();”命令,我看到我主页内容上面的文字,所以查看文件就OK了。为什么我只看到一个空白的白页?
  • 这是迄今为止最简单的解决方案,但是对于那些正在接收空白页面的人(如我),如果您将第三个参数设置为 TRUE,您可以检索 HTML 然后回显它。 $content = $this->load->view('maintenance_view, '', TRUE);回声$内容;
【解决方案3】:

这是我的解决方案,简单、干净、有效地适用于所有 url 调用和 SEO 方面:


将此变量添加到: application/config/config.php

$config['maintenance_mode'] = TRUE;
$config['maintenance_ips'] = array('0.0.0.0', '1.1.1.1', '2.2.2.2');

在末尾添加此条件: application/config/routes.php

if(!in_array($_SERVER['REMOTE_ADDR'], $this->config->item('maintenance_ips')) && $this->config->item('maintenance_mode')) {
    $route['default_controller'] = "your_controller/maintenance";
    $route['(:any)'] = "your_controller/maintenance";";
}

创建方法维护在: 应用程序/控制器/your_controller.php

function maintenance() {
     $this->output->set_status_header('503'); 
     $this->load->view('maintenance_view');
}

创建视图: application/views/maintenance_view.php

<!DOCTYPE html>
<html>
   <head>
      <title>Maintenance</title>
   </head>
   <body>
      <p>We apologize but our site is currently undergoing maintenance at this time.</p>
      <p>Please check back later.</p>
   </body>
</html>

【讨论】:

    【解决方案4】:

    这是我想出的创建维护模式的方法。

    1. 在 config.php 文件中启用 Hooks
    2. 在errors文件夹下创建error_maintenance.php页面
    3. 创建一个名为维护的 Hook
    4. 在 hooks 配置设置中,您的 hooks 调用要在 post_controller 上运行

    应用程序/错误/error_maintenance.php

    <!DOCTYPE html>
    <html>
      <head>
        <title>Maintenance</title>
        <style>Style your page</style>
      </head>
      <body>
        <p>We apologize but our site is currently undergoing maintenance at this time.</p>
        <p>Please check back later.</p>
      </body>
    </html>
    

    应用程序/钩子/maintenance.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class maintenance
    {
       var $CI;    
       public function maintenance()
       {
          $this->CI =& get_instance();
          $this->CI->load->config("config_maintenance");    
          if(config_item("maintenance"))
          {
              $_error =& load_class('Exceptions', 'core');
              echo $_error->show_error("", "", 'error_maintenance', 200);
              exit;
          }
       }
    }
    

    应用程序/config/hooks.php

    $hook['post_controller'][] = array(
       'class' => 'maintenance',
       'function' => 'maintenance',
       'filename' => 'maintenance.php',
       'filepath' => 'hooks',
       'params' => array()
    );
    

    【讨论】:

      【解决方案5】:

      这个怎么样:

      1. 创建自动加载的库,始终检查数据库上的维护标志。
      2. 创建一个模块来控制您的应用程序维护标志。
      3. 创建一个模块以在维护模式开启时进行重定向

      自动加载的库可以包含这样的内容:

      class Maintenance_mode {
          function __construct(){
              $CI =& get_instance();
              $check_maintenance = $CI->db->select('flag_mode')->get('tbl_settings')->result();
              if($check_maintenance[0]->flag_mode == '1') 
                  redirect(site_url('maintenance_mode_controller'));
          }
      }
      

      下一步是为维护页面创建一个控制器。

      【讨论】:

        【解决方案6】:

        这个很好用,

        application/views/vw_maintenance.php

            <!DOCTYPE html>
            <html>
              <head>
                <title>Maintenance</title>
                <style>Style your page</style>
              </head>
              <body>
                <p>We apologize but our site is currently undergoing maintenance at this time.</p>
                <p>Please check back later.</p>
              </body>
            </html>
        <?php exit(); ?>
        

        exit()函数很重要,别忘了把它放在底部,它会阻止所有页面显示。

        application/libraries/maintenance.php

        class Maintenance{
        
            private $CI;
        
            public function __construct() {
        
                $this->CI =& get_instance();
        
                // flag on and off
                $this->flag( $this->CI->uri->segment(1) );
        
                // get the flag status
                $check_maintenance = $this->CI->db->select('flag_mode')->where(array('setting_name' => 'maintenance'))->get('tbl_settings')->result();
        
                //display view if true
                if($check_maintenance[0]->flag_mode == '1')
                    $this->CI->load->view('vw_maintenance');
        
        
            }
        
            private function flag($command){
        
        
                $this->CI->db->where('setting_name', 'evolving');
        
                switch($command){
        
                    case "activate":                
                        $this->CI->db->update('tbl_settings', array('flag_mode' => 1) ); 
                    break;
        
                    case "deactivate":
                        $this->CI->db->update('tbl_settings', array('flag_mode' => 0) );
                        redirect(site_url('/'));
                    break;
        
                }
            }
        
        }
        

        自动加载库,以便检查每个页面加载。

        现在您可以通过键入或来激活和停用维护模式

        【讨论】:

          【解决方案7】:

          我认为这很简单,只需像下面这样在构造函数上调用视图。

          评论(网站将上线)

          class home extends CI_Controller {
          
          public function __construct() {
              parent::__construct();
              //echo $this->load->view('maintenance','',true);exit;
              }
          }
          

          取消注释(网站将在维护中)

          class home extends CI_Controller {
          
          public function __construct() {
              parent::__construct();
              echo $this->load->view('maintenance','',true);exit;
              }
          }
          

          您可以在 CI 应用程序的“视图”下使用自己的“maintenance.php”页面。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-06-20
            • 1970-01-01
            • 1970-01-01
            • 2010-11-26
            • 1970-01-01
            相关资源
            最近更新 更多