2013-08-04 36 views
1

我正在爲我的CodeIgniter項目編寫一個template_loader。像往常一樣,我需要幾個安全層爲我的模板。其中一個,即第一個,是檢查文件是否存在。CodeIgniter檢查文件

現在,我的問題是:我無法配置給模板加載器的地址。當我使用名爲'include()'的簡單php函數時,它可以工作,但是使用我的template_loader函數,它無法工作。

這裏是我的實際網頁(的index.php):

<?php 
    /** 
    Add the page-suitable template from in folder. 
    **/ 
    $this->template_loader->load_template('inc/temp_one_col.php'); 
    // include('inc/temp_one_col.php'); 

?> 

這裏是我的階級和template_loader:

class Template_loader 
{ 
    function load_template ($arg) 
    { 
     $base_name = basename($arg); 

     $CI =& get_instance(); 

     if(file_exists($arg) === true) 
     {      
       echo 'it is also good.'; 
       if (pathinfo($base_name, PATHINFO_EXTENSION) == 'php' || 
       pathinfo($base_name, PATHINFO_EXTENSION) == 'html' 
       ) 
       {     
        $file_name_check = substr($base_name, 0, 4); 
        if($file_name_check === TEMP_FILE_INDICATOR) 
        {      
         include($arg); 
        } 
        else 
        { 
         redirect($CI->base_url . '/error/show_problem');      
        } 
       } 
       else 
       { 
       redirect($CI->base_url . '/error/show_problem');  

       } 
     } 
     else 
     { 
      redirect($CI->base_url . '/error/show_problem');    
     }  
    } 
} 
+0

請參閱http://stackoverflow.com/questions/6188727/how -to-add-autoload-function-to-codeigniter(用於自動加載功能,標記爲答案) – Kyslik

+0

對不起,你好像誤解了我的問題,或者我誤解了你的回覆。我已經在控制器中加載了庫,唯一的錯誤是地址問題。因爲file_exists沒有找到參數中給出的文件。 –

+0

哦,我看到了,試試'base_url($ arg);'('base_url()'在url helper中)。我相信,只要給'file_exists()'提供了一個錯誤的路徑,甚至可以嘗試使用CIs constanst作爲'APPPATH' ..在這裏查看完整列表http://ellislab.com/codeigniter/user-guide/general/reserved_names .html – Kyslik

回答

2

出於興趣,你傳遞給函數作爲$arg參數?

聽起來像你只需要使用文件的正確路徑,這應該是文件系統中文件的絕對路徑。

要獲得絕對路徑,您可以在您的網站index.php中創建一個新的全局變量,以指向您的視圖文件夾。

的webroot/index.php文件:

if (realpath('../application/views') !== FALSE) 
{ 
    $views_path =realpath('../application/views').'/'; 
    define('VIEWSPATH', $views_path); 
} 

現在你可以使用這個爲基準,爲您$arg參數VIEWSPATH.$path_to_file