如何检查视图是否存在?

Cod*_*ypt 2 php codeigniter

在CodeIgniter中我试图在加载视图之前检查文件是否存在于控制器,我试过像 -

public function foo($file = "")
{
    if (file_exists(APPPATH."views/log/{$file}.".EXT))
    {
        $this->load->view('log/'.$file);
    }
    else
        show_404();
}
Run Code Online (Sandbox Code Playgroud)

但是文件存在那里然后file_exists()方法也返回了false,我也用它来检查它var_dump().我不知道这里有什么问题,我猜测有一些与映射有关的问题,因为目录结构就像 -

application
   |--controllers
   |  |--ctrl.php   //Controller file where I am checking
   |--views
      |--log
         |-- .. //Checking files for here
Run Code Online (Sandbox Code Playgroud)

但我怎么解决?

编辑:错误输入'views'到'view'(在我调用file_exists()方法的代码中if)纠正了它.

Wes*_*rch 6

EXTCodeigniter中的常量等于.php并定义于index.php:

// The PHP file extension
// this global constant is deprecated.
define('EXT', '.php');
Run Code Online (Sandbox Code Playgroud)

您正在添加一个额外的点:

APPPATH."views/log/{$file}.".EXT
//                        ^
Run Code Online (Sandbox Code Playgroud)

这会产生类似的结果 views/log/myfile..php

由于常数正在逐步淘汰,你应该避免它.

  • 将它分配给变量更有意义,然后检查该变量的值.好的,祝你好运前进. (3认同)