1

I'm writing a template_loader for my CodeIgniter project. As usual, I need several security layers for my templates. One of them, which is the very first one, is checking if the files exists or not.

Now, My Problem is: I can't configure what address to give to the template loader. When I use simple php function called 'include()', it works, but with my template_loader function, it fails to work.

Here is my actual page (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');

?>

And here is my class and 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');            
        }       
    }
}
4

1 回答 1

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

于 2013-08-05T04:25:12.197 回答