如何在笨“维护模式”构建?(How to build in 'maintenance mod

2019-07-22 17:17发布

我使用的是最新的笨,我需要创建一个标志(最好在配置)时,转向“真”,所有页面显示“维护模式”消息,而不是执行其控制代码。

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

Answer 1:

通过将一个新的文件中被称为MY_Controller核心目录扩展是CI_Controller。

在这个文件中的构造函数,做这样的事情:

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

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

让应用中的所有控制器从类继承。



Answer 2:

在这里我的解决方案,为我工作得很好:

下面将立即打电话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();

    }
}

添加文件Maintanance.php在同一文件夹中的index.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');
?>


Answer 3:

在这里我的解决方案,简单,干净,有效,对所有URL调用和SEO方面:


增加这个变量: 应用程序/配置/ config.php文件

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

添加这个条件在结束: 应用程序/配置/ 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');
}

创建视图: 应用程序/视图/ 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>


Answer 4:

这个怎么样 :

  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'));
    }
}

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



Answer 5:

这里是我想出了创建维护模式。

  1. 在config.php文件启用鱼钩
  2. 创建错误文件夹下的error_maintenance.php页
  3. 创建一个名为钩维修
  4. 在钩配置设置你的钩子调用上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;
      }
   }
}

应用/配置/ hooks.php

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


Answer 6:

这个效果很好,

应用/视图/ 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()函数非常importantn不要忘了把它的底部,它会阻止所有页面的显示。

应用/库/ 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;

        }
    }

}

自动加载磁带库所以它会检查每个页面加载。

现在你可以激活并通过键入关闭维护模式或



文章来源: How to build in 'maintenance mode' in Codeigniter?