gpt4 book ai didi

php - 如何检查 View 是否存在?

转载 作者:行者123 更新时间:2023-12-02 23:39:39 24 4
gpt4 key购买 nike

在 CodeIgniter 中,我尝试在从 Controller 加载 View 之前检查文件是否存在,我尝试过 -

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

但是文件存在,那么 file_exists() 方法也返回 false,我也使用 var_dump() 检查了它。我不明白这里有什么问题,我猜测存在与映射相关的问题,因为目录结构类似于 -

application
|--controllers
| |--ctrl.php //Controller file where I am checking
|--views
|--log
|-- .. //Checking files for here

但是我该如何解决呢?

编辑:只是将“views”错误地输入为“view”(在我在 if 中调用 file_exists() 方法的代码中)更正了它。

最佳答案

Codeigniter 中的 EXT 常量等于 .php 并在 index.php 中定义:

// The PHP file extension
// this global constant is deprecated.
define('EXT', '.php');

您要添加一个额外的点:

APPPATH."views/log/{$file}.".EXT
// ^

这会导致类似于 views/log/myfile..php

由于该常量正在被逐步淘汰,因此您应该避免使用它。

关于php - 如何检查 View 是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19711815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com