如何在codeigniter中获取视图文件的路径

Muh*_*eel 3 codeigniter view path

我想询问是否有任何方法可以在控制器中获取我的视图文件的路径.例如

class welcome extends controller{

  function __construct(){
       parent::__construct();
  }
  function index(){
     $this->load->view('welcome_message');
  }
  function test(){
     $my_variable = $this->load->view('welcome_message','',TRUE);
  }
  function another_test(){
        ///  $path_to_view = ???;
        ///  echo $path_to_view;
  } 
}
Run Code Online (Sandbox Code Playgroud)

我想问一下是否有任何辅助函数来实现这个目的.测试方法包含包含html内容的变量.但我想得到视图文件的路径???

The*_*pha 8

我不确定这是否是一种正确的方法,但你可以尝试这个,只需创建一个帮助文件,即my_helper.php在你的application/helper文件夹中并粘贴以下函数在这个帮助文件中

function get_view_path($view_name)
{
    $target_file=APPPATH.'views/'.$view_name.'.php';
    if(file_exists($target_file)) return $target_file;
}
Run Code Online (Sandbox Code Playgroud)

要使用它,您必须首先加载帮助文件,然后使用视图名称作为函数的参数调用该函数

$this->load->helper('my_helper');
$path_to_view = get_view_path('welcome'); // Will return the path if welcome.php exists in the view folder.
Run Code Online (Sandbox Code Playgroud)

您可以使用config.php自动加载它 $autoload['helper'] = array('functions_helper');


小智 7

它应该是常数:

VIEWPATH
Run Code Online (Sandbox Code Playgroud)