2013-08-04 37 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