doujiao7325 2016-06-05 20:27
浏览 24
已采纳

Third_party Views Path(s)签入CodeIgniter 3

I'm trying to check if a view file really exists according to the third_party paths loaded

Normally, I'd check if a view exists with is_file(APPPATH.'/views/folder/'.$view)

I can retrieve every loaded third_party paths with get_package_paths (thanks to the comment of Tpojka) and then check in their folder views if the file exists,

but I was hoping for a 'direct' check, as if the ->view function would return false instead of redirecting to an error page

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

Though I realize there might be no other solutions that adding this manual check with a loop through the loaded paths and hide it in a CI_Load Class extension (application/core/MY_Loader) to give the apparance of a direct check in the controller:

EDIT: This is a bad idea, cause view() may return false to CI function that might not be designed for

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;
}
}

What I find annoying is that load->view already makes a check through the paths, so this solution will add a second check and increase server consumption..

  • 写回答

1条回答 默认 最新

  • dthh5038 2016-06-10 14:40
    关注

    Well in the end I choose this lukewarm solution :

    Instead of extending the function view() for it to return false (and having to deal with it through CI then after !) , I just made a function is_view() in the application/core/MY_Loader.php

    I'm not sure MY_Loader is the correct place to place such a function, but so far it did the trick for me ...

    (thx Tpojka for the indication)

    in 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;
    }
    

    and in 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 !";
    

    and of course in the view

    <h1>My Frame</h1>
    <p>
        The path of this file is <=?$view_path?>
    </p>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测