cari
Rumahpembangunan bahagian belakangtutorial phpCodeigniter 生成静态页面_PHP教程

Codeigniter 生成静态页面_PHP教程

Jul 14, 2016 am 10:09 AM
codeignitergunamenjanaMudahforummembacastatikmuka surat

    使用CI来生成静态页面,其实很简单,就像论坛里面说的那样,读出页面中的数据,再写入html文件中,最后显示这个html文件就行了,好吧,上码。

 
 
[php] 
                
class MY_Loader extends CI_Loader {  
                    
    public function m_view($view, $vars = array(), $return = FALSE){  
        return $this->_m_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));  
    }  
                    
    protected function _m_ci_load($_ci_data){  
          .....  
                      
            $_ci_html_file=($_ci_ext==='')? $_ci_view.".html" : $_ci_view;//这,生成静态页面的文件名   
                            
            foreach ($this->_ci_view_paths as $_ci_view_file => $cascade){  
                if (file_exists($_ci_view_file.$_ci_file)){  
                    $_ci_path = $_ci_view_file.$_ci_file;  
                    $_ci_html_path=$_ci_view_file.$_ci_html_file;//生成静态页面的路径   
                    $file_exists = TRUE;  
                    break;  
                }  
      ......  
            }  
        }  
                
     .......  
        //在这   
      if(config_item("html")===TRUE){//是否开启生成静态页面   
            $_html_file=@fopen($_ci_html_path,'r');//创建.html文件   
            $buffer = ob_get_contents();  
            @ob_end_clean();  
            if(!$_html_file||(@filesize($_ci_html_path)!=strlen($buffer))){ //如果文件不存在或文件已更变   
                $_html_file=@fopen($_ci_html_path,'w');  
                flock($_html_file, LOCK_EX);  
                fwrite($_html_file, $buffer);                    
                flock($_html_file, LOCK_UN);  
                fclose($_html_file);  
            }  
            //echo(filesize($_ci_html_path)."-".strlen($buffer));   
            include($_ci_html_path);  
        }  
                            
   ......  
    }    
}  
 
              
class MY_Loader extends CI_Loader {
                  
    public function m_view($view, $vars = array(), $return = FALSE){
        return $this->_m_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
    }
                  
    protected function _m_ci_load($_ci_data){
          .....
                    
            $_ci_html_file=($_ci_ext==='')? $_ci_view.".html" : $_ci_view;//这,生成静态页面的文件名
                          
            foreach ($this->_ci_view_paths as $_ci_view_file => $cascade){
                if (file_exists($_ci_view_file.$_ci_file)){
                    $_ci_path = $_ci_view_file.$_ci_file;
                    $_ci_html_path=$_ci_view_file.$_ci_html_file;//生成静态页面的路径
                    $file_exists = TRUE;
                    break;
                }
      ......
            }
        }
              
     .......
        //在这
      if(config_item("html")===TRUE){//是否开启生成静态页面
            $_html_file=@fopen($_ci_html_path,'r');//创建.html文件
            $buffer = ob_get_contents();
            @ob_end_clean();
            if(!$_html_file||(@filesize($_ci_html_path)!=strlen($buffer))){ //如果文件不存在或文件已更变
                $_html_file=@fopen($_ci_html_path,'w');
                flock($_html_file, LOCK_EX);
                fwrite($_html_file, $buffer);                  
                flock($_html_file, LOCK_UN);
                fclose($_html_file);
            }
            //echo(filesize($_ci_html_path)."-".strlen($buffer));
            include($_ci_html_path);
        }
                          
   ......
    }  
}调用
 
 
[html]  
$this->load->m_view('login',$datas);  
 
$this->load->m_view('login',$datas);
是否生成HTML文件
 
$config["html"]                =  TRUE;
 
 
 
 
全部代码如下
 
 
[php] 
         
class MY_Loader extends CI_Loader {  
             
    public function m_view($view, $vars = array(), $return = FALSE){  
        return $this->_m_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));  
    }  
             
    protected function _m_ci_load($_ci_data){  
        // Set the default data variables   
        foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val){  
            $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;  
        }  
         
        $file_exists = FALSE;  
        // Set the path to the requested file   
        if (is_string($_ci_path) && $_ci_path !== ''){  
            $_ci_x = explode('/', $_ci_path);//使用一个字符串分割另一个字符串   
            $_ci_file = end($_ci_x);//将数组的内部指针指向最后一个单元   
        }else{  
            $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);// 返回文件路径的信息   
            $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;  
            $_ci_html_file=($_ci_ext==='')? $_ci_view.".html" : $_ci_view;//这,生成静态页面的文件名   
                     
            foreach ($this->_ci_view_paths as $_ci_view_file => $cascade){  
                if (file_exists($_ci_view_file.$_ci_file)){  
                    $_ci_path = $_ci_view_file.$_ci_file;  
                    $_ci_html_path=$_ci_view_file.$_ci_html_file;//生成静态页面的路径   
                    $file_exists = TRUE;  
                    break;  
                }  
         
                if ( ! $cascade){  
                    break;  
                }  
            }  
        }  
         
        if ( ! $file_exists && ! file_exists($_ci_path))  
        {  
            show_error('Unable to load the requested file: '.$_ci_file);  
        }  
         
        // This allows anything loaded using $this->load (views, files, etc.)   
        // to become accessible from within the Controller and Model functions.   
        $_ci_CI =& get_instance();  
        foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)  
        {  
            if ( ! isset($this->$_ci_key))  
            {  
                $this->$_ci_key =& $_ci_CI->$_ci_key;  
            }  
        }  
         
        /* 
         * Extract and cache variables 
         * 
         * You can either set variables using the dedicated $this->load->vars() 
         * function or via the second parameter of this function. We'll merge 
         * the two types and cache them so that views that are embedded within 
         * other views can have access to these variables. 
         */  
        if (is_array($_ci_vars))  
        {  
            $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);  
        }  
        extract($this->_ci_cached_vars);  
         
        /* 
         * Buffer the output 
         * 
         * We buffer the output for two reasons: 
         * 1. Speed. You get a significant speed boost. 
         * 2. So that the final rendered template can be post-processed by 
         *  the output class. Why do we need post processing? For one thing, 
         *  in order to show the elapsed page load time. Unless we can 
         *  intercept the content right before it's sent to the browser and 
         *  then stop the timer it won't be accurate. 
         */  
        ob_start();  
         
        // If the PHP installation does not support short tags we'll   
        // do a little string replacement, changing the short tags   
        // to standard PHP echo statements.   
        if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE  
            && config_item('rewrite_short_tags') === TRUE && function_usable('eval')  
        )  
        {  
            echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('=', '
        }  
        else  
        {  
            include($_ci_path); // include() vs include_once() allows for multiple views with the same name   
        }  
         
        log_message('debug', 'File loaded: '.$_ci_path);  
         
        // Return the file data if requested   
        if ($_ci_return === TRUE)  
        {  
            $buffer = ob_get_contents();  
            @ob_end_clean();  
            return $buffer;  
        }  
        //在这   
         if(config_item("html")===TRUE){//是否开启生成静态页面   
            $_html_file=@fopen($_ci_html_path,'r');//创建.html文件   
            $buffer = ob_get_contents();  
            @ob_end_clean();  
            if(!$_html_file||(@filesize($_ci_html_path)!=strlen($buffer))){  
                       $_html_file=@fopen($_ci_html_path,'w');  
                       flock($_html_file, LOCK_EX);  
                fwrite($_html_file, $buffer);                    
                flock($_html_file, LOCK_UN);  
                fclose($_html_file);  
            }  
            //echo(filesize($_ci_html_path)."-".strlen($buffer));   
            include($_ci_html_path);  
        }  
                     
         
        /* 
         * Flush the buffer... or buff the flusher? 
         * 
         * In order to permit views to be nested within 
         * other views, we need to flush the content back out whenever 
         * we are beyond the first level of output buffering so that 
         * it can be seen and included properly by the first included 
         * template and any subsequent ones. Oy! 
         */  
        if (ob_get_level() > $this->_ci_ob_level + 1)  
        {  
            ob_end_flush();  
        }  
        else  
        {  
            $_ci_CI->output->append_output(ob_get_contents());  
            @ob_end_clean();  
        }  
    }    
}  
 
       
class MY_Loader extends CI_Loader {
           
    public function m_view($view, $vars = array(), $return = FALSE){
        return $this->_m_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
    }
           
    protected function _m_ci_load($_ci_data){
        // Set the default data variables
        foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val){
            $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
        }
       
        $file_exists = FALSE;
        // Set the path to the requested file
        if (is_string($_ci_path) && $_ci_path !== ''){
            $_ci_x = explode('/', $_ci_path);//使用一个字符串分割另一个字符串
            $_ci_file = end($_ci_x);//将数组的内部指针指向最后一个单元
        }else{
            $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);// 返回文件路径的信息
            $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
            $_ci_html_file=($_ci_ext==='')? $_ci_view.".html" : $_ci_view;//这,生成静态页面的文件名
                   
            foreach ($this->_ci_view_paths as $_ci_view_file => $cascade){
                if (file_exists($_ci_view_file.$_ci_file)){
                    $_ci_path = $_ci_view_file.$_ci_file;
                    $_ci_html_path=$_ci_view_file.$_ci_html_file;//生成静态页面的路径
                    $file_exists = TRUE;
                    break;
                }
       
                if ( ! $cascade){
                    break;
                }
            }
        }
       
        if ( ! $file_exists && ! file_exists($_ci_path))
        {
            show_error('Unable to load the requested file: '.$_ci_file);
        }
       
        // This allows anything loaded using $this->load (views, files, etc.)
        // to become accessible from within the Controller and Model functions.
        $_ci_CI =& get_instance();
        foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
        {
            if ( ! isset($this->$_ci_key))
            {
                $this->$_ci_key =& $_ci_CI->$_ci_key;
            }
        }
       
        /*
         * Extract and cache variables
         *
         * You can either set variables using the dedicated $this->load->vars()
         * function or via the second parameter of this function. We'll merge
         * the two types and cache them so that views that are embedded within
         * other views can have access to these variables.
         */
        if (is_array($_ci_vars))
        {
            $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
        }
        extract($this->_ci_cached_vars);
       
        /*
         * Buffer the output
         *
         * We buffer the output for two reasons:
         * 1. Speed. You get a significant speed boost.
         * 2. So that the final rendered template can be post-processed by
         *  the output class. Why do we need post processing? For one thing,
         *  in order to show the elapsed page load time. Unless we can
         *  intercept the content right before it's sent to the browser and
         *  then stop the timer it won't be accurate.
         */
        ob_start();
       
        // If the PHP installation does not support short tags we'll
        // do a little string replacement, changing the short tags
        // to standard PHP echo statements.
        if ( ! is_php('5.4') && (bool) @ini_get('short_open_tag') === FALSE
            && config_item('rewrite_short_tags') === TRUE && function_usable('eval')
        )
        {
            echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('=', '
        }
        else
        {
            include($_ci_path); // include() vs include_once() allows for multiple views with the same name
        }
       
        log_message('debug', 'File loaded: '.$_ci_path);
       
        // Return the file data if requested
        if ($_ci_return === TRUE)
        {
            $buffer = ob_get_contents();
            @ob_end_clean();
            return $buffer;
        }
        //在这
         if(config_item("html")===TRUE){//是否开启生成静态页面
            $_html_file=@fopen($_ci_html_path,'r');//创建.html文件
            $buffer = ob_get_contents();
            @ob_end_clean();
            if(!$_html_file||(@filesize($_ci_html_path)!=strlen($buffer))){
                       $_html_file=@fopen($_ci_html_path,'w');
                       flock($_html_file, LOCK_EX);
                fwrite($_html_file, $buffer);                  
                flock($_html_file, LOCK_UN);
                fclose($_html_file);
            }
            //echo(filesize($_ci_html_path)."-".strlen($buffer));
            include($_ci_html_path);
        }
                   
       
        /*
         * Flush the buffer... or buff the flusher?
         *
         * In order to permit views to be nested within
         * other views, we need to flush the content back out whenever
         * we are beyond the first level of output buffering so that
         * it can be seen and included properly by the first included
         * template and any subsequent ones. Oy!
         */ www.2cto.com
        if (ob_get_level() > $this->_ci_ob_level + 1)
        {
            ob_end_flush();
        }
        else
        {
            $_ci_CI->output->append_output(ob_get_contents());
            @ob_end_clean();
        }
    }  
}
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477728.htmlTechArticle使用CI来生成静态页面,其实很简单,就像论坛里面说的那样,读出页面中的数据,再写入html文件中,最后显示这个html文件就行了,好吧,上码。...
Kenyataan
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
PHP vs Python: Memahami PerbezaanPHP vs Python: Memahami PerbezaanApr 11, 2025 am 12:15 AM

PHP dan Python masing -masing mempunyai kelebihan sendiri, dan pilihannya harus berdasarkan keperluan projek. 1.Php sesuai untuk pembangunan web, dengan sintaks mudah dan kecekapan pelaksanaan yang tinggi. 2. Python sesuai untuk sains data dan pembelajaran mesin, dengan sintaks ringkas dan perpustakaan yang kaya.

PHP: Adakah ia mati atau hanya menyesuaikan diri?PHP: Adakah ia mati atau hanya menyesuaikan diri?Apr 11, 2025 am 12:13 AM

PHP tidak mati, tetapi sentiasa menyesuaikan diri dan berkembang. 1) PHP telah menjalani beberapa lelaran versi sejak tahun 1994 untuk menyesuaikan diri dengan trend teknologi baru. 2) Ia kini digunakan secara meluas dalam e-dagang, sistem pengurusan kandungan dan bidang lain. 3) Php8 memperkenalkan pengkompil JIT dan fungsi lain untuk meningkatkan prestasi dan pemodenan. 4) Gunakan OPCACHE dan ikut piawaian PSR-12 untuk mengoptimumkan prestasi dan kualiti kod.

Masa Depan PHP: Adaptasi dan InovasiMasa Depan PHP: Adaptasi dan InovasiApr 11, 2025 am 12:01 AM

Masa depan PHP akan dicapai dengan menyesuaikan diri dengan trend teknologi baru dan memperkenalkan ciri -ciri inovatif: 1) menyesuaikan diri dengan pengkomputeran awan, kontena dan seni bina microservice, menyokong Docker dan Kubernetes; 2) memperkenalkan pengkompil JIT dan jenis penghitungan untuk meningkatkan prestasi dan kecekapan pemprosesan data; 3) Berterusan mengoptimumkan prestasi dan mempromosikan amalan terbaik.

Bilakah anda menggunakan sifat berbanding kelas abstrak atau antara muka dalam PHP?Bilakah anda menggunakan sifat berbanding kelas abstrak atau antara muka dalam PHP?Apr 10, 2025 am 09:39 AM

Dalam PHP, sifat sesuai untuk situasi di mana penggunaan semula kaedah diperlukan tetapi tidak sesuai untuk warisan. 1) Ciri membolehkan kaedah multiplexing dalam kelas untuk mengelakkan pelbagai kerumitan warisan. 2) Apabila menggunakan sifat, anda perlu memberi perhatian kepada konflik kaedah, yang dapat diselesaikan melalui alternatif dan sebagai kata kunci. 3) Tua yang berlebihan harus dielakkan dan tanggungjawab tunggalnya harus dikekalkan untuk mengoptimumkan prestasi dan meningkatkan pemeliharaan kod.

Apakah bekas suntikan ketergantungan (DIC) dan mengapa menggunakan satu dalam PHP?Apakah bekas suntikan ketergantungan (DIC) dan mengapa menggunakan satu dalam PHP?Apr 10, 2025 am 09:38 AM

Kontena Suntikan Ketergantungan (DIC) adalah alat yang menguruskan dan menyediakan kebergantungan objek untuk digunakan dalam projek PHP. Manfaat utama DIC termasuk: 1. Decoupling, membuat komponen bebas, dan kod itu mudah dikekalkan dan diuji; 2. Fleksibiliti, mudah untuk menggantikan atau mengubah suai kebergantungan; 3. Keseluruhan, mudah untuk menyuntik objek mengejek untuk ujian unit.

Terangkan SPL SPLFixedArray dan ciri -ciri prestasinya berbanding dengan susunan PHP biasa.Terangkan SPL SPLFixedArray dan ciri -ciri prestasinya berbanding dengan susunan PHP biasa.Apr 10, 2025 am 09:37 AM

SplfixedArray adalah pelbagai saiz tetap dalam PHP, sesuai untuk senario di mana prestasi tinggi dan penggunaan memori yang rendah diperlukan. 1) Ia perlu menentukan saiz apabila membuat untuk mengelakkan overhead yang disebabkan oleh pelarasan dinamik. 2) Berdasarkan pelbagai bahasa C, secara langsung mengendalikan memori dan kelajuan akses cepat. 3) Sesuai untuk pemprosesan data berskala besar dan persekitaran sensitif memori, tetapi ia perlu digunakan dengan berhati-hati kerana saiznya tetap.

Bagaimana PHP mengendalikan fail memuat naik dengan selamat?Bagaimana PHP mengendalikan fail memuat naik dengan selamat?Apr 10, 2025 am 09:37 AM

PHP mengendalikan fail muat naik melalui pembolehubah fail $ \ _. Kaedah untuk memastikan keselamatan termasuk: 1. Semak kesilapan muat naik, 2. Sahkan jenis dan saiz fail, 3. Mencegah penindasan fail, 4. Pindahkan fail ke lokasi storan tetap.

Apakah pengendali pengendali coalescing null (??) dan pengendali tugasan comelan null (?? =)?Apakah pengendali pengendali coalescing null (??) dan pengendali tugasan comelan null (?? =)?Apr 10, 2025 am 09:33 AM

Dalam JavaScript, anda boleh menggunakan NullcoalescingOperator (??) dan NullcoalescingAssignmentOperator (?? =). 1.? Menerapkan semula operan pertama yang tidak berselisih atau tidak ditentukan. 2.?? Pengendali ini memudahkan logik kod, meningkatkan kebolehbacaan dan prestasi.

See all articles

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Artikel Panas

R.E.P.O. Kristal tenaga dijelaskan dan apa yang mereka lakukan (kristal kuning)
3 minggu yang laluBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Tetapan grafik terbaik
3 minggu yang laluBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Cara Memperbaiki Audio Jika anda tidak dapat mendengar sesiapa
3 minggu yang laluBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Cara Membuka Segala -galanya Di Myrise
3 minggu yang laluBy尊渡假赌尊渡假赌尊渡假赌

Alat panas

EditPlus versi Cina retak

EditPlus versi Cina retak

Saiz kecil, penyerlahan sintaks, tidak menyokong fungsi gesaan kod

SublimeText3 Linux versi baharu

SublimeText3 Linux versi baharu

SublimeText3 Linux versi terkini

Versi Mac WebStorm

Versi Mac WebStorm

Alat pembangunan JavaScript yang berguna

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Muat turun versi mac editor Atom

Muat turun versi mac editor Atom

Editor sumber terbuka yang paling popular