【问题标题】:Third_party Views Path(s) Check in CodeIgniter 3在 CodeIgniter 3 中检查第三方视图路径
【发布时间】:2016-06-05 20:27:04
【问题描述】:

我正在尝试根据加载的第三方路径检查视图文件是否真的存在

通常,我会使用is_file(APPPATH.'/views/folder/'.$view) 检查视图是否存在

我可以使用get_package_paths 检索每个加载的第三方路径(感谢 Tpojka 的评论),然后检查他们的文件夹视图(如果文件存在),

但我希望进行“直接”检查,好像 ->view 函数会返回 false 而不是重定向到错误页面

$html = $this->load->view($tpl,'',TRUE) ? $this->load->view($tpl,'',TRUE) : $another_template;

虽然我意识到可能没有其他解决方案可以通过加载的路径循环添加此手动检查并将其隐藏在 CI_Load 类扩展 (application/core/MY_Loader) 中以在控制器中提供直接检查的外观:

编辑:这是个坏主意,因为 view() 可能会将 false 返回到可能不是为之设计的 CI 函数

class MY_Loader extends CI_Lodaer{

public function __construct() {

    parent::__construct();

}

public function view($view, $vars = array(), $return = FALSE)
{
    foreach( $this->get_package_paths( TRUE ) as $path )
    {
        // this will only retrieve html from the first file found
        if( is_file( $path."/views/".$view ) ) return parent::view($view, $vars, $return);
    }
    // if no match
    return false;
}
}

我觉得烦人的是 load->view 已经对路径进行了检查,所以这个解决方案将添加第二次检查并增加服务器消耗..

【问题讨论】:

  • 可以get_package_paths帮忙吗?
  • 这是一个好的开始,对,谢谢你的提示!我仍然必须手动检查 third_party 视图文件夹中的文件,但它确实可以很容易地交叉结果。
  • 其实我想我到底会怎么做;虽然我希望有一种可能的直接检查(也许通过扩展 load->view ...)
  • 我很高兴我能指出一些有用的东西,虽然我不明白为什么你不能使用is_file($file)。如果包文件存在,你想加载包文件,如果包文件不存在,你想加载主视图文件?

标签: php codeigniter codeigniter-3


【解决方案1】:

最后我选择了这个不冷不热的解决方案:

我没有扩展函数 view() 让它返回 false (并且必须在之后通过 CI 处理它!),我只是在 application/core/MY_Loader.php 中创建了一个函数 is_view()

我不确定 MY_Loader 是否是放置此类函数的正确位置,但到目前为止它对我有用...

(感谢 Tpojka 的指示)

在 application/core/MY_Loader.php 中

/**
 * is_view
 *
 * Check if a view exists or not through the loaded paths
 *
 * @param   string          $view           The relative path of the file
 *
 * @return  string|bool     string          containing the path if file exists
 *                          false           if file is not found
 */
public function is_view($view)
{
    // ! BEWARE $path contains a beginning trailing slash !
    foreach( $this->get_package_paths( TRUE ) as $path )
    {
        // set path, check if extension 'php' 
        // (would be better using the constant/var defined for file extension of course)
        $path_file = ( strpos($view,'.php') === false ) ?  $path."views/".$view.'.php' : $path."views/".$view ;

        // this will return the path at first match found
        if( is_file( $path_file ) ) return $path."views/";
    }
    // if no match
    return false;
}

在 application/controllers/Welcome.php 中

$view = "frames/my_html.php";

/*
 *   the view file should be in 
 *   application/third_party/myapp/views/frames/my_html.php
 *   
 *   so far, if the file does not exists, and we try 
 *   $this->load->view($view) will redirect to an error page
*/

// check if view exists and retrieve path
if($possible_path = $this->load->is_view($view)) 
{
    //set the data array
    $data = array("view_path"=>$possible_path);

    // load the view knowing it exists
    $this->load->view($view,$data)
}
else echo "No Template for this frame in any Paths !";

当然是在视图中

<h1>My Frame</h1>
<p>
    The path of this file is <=?$view_path?>
</p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 2012-07-19
    • 2018-08-15
    • 1970-01-01
    相关资源
    最近更新 更多