Third_party 查看路径检查 CodeIgniter 3
Third_party Views Path(s) Check in CodeIgniter 3
我正在尝试根据加载的 third_party 路径检查视图文件是否确实存在
通常,我会检查视图是否存在 is_file(APPPATH.'/views/folder/'.$view)
我可以使用 get_package_paths 检索每个加载的 third_party 路径(感谢 Tpojka 的评论),然后检查它们的文件夹视图是否存在文件,
但我希望进行 'direct' 检查,就好像 ->view
函数会 return false
而不是重定向到错误页面
$html = $this->load->view($tpl,'',TRUE) ? $this->load->view($tpl,'',TRUE) : $another_template;
虽然我意识到可能没有其他解决方案可以通过加载路径添加此手动检查并将其隐藏在 CI_Load Class 扩展名 (application/core/MY_Loader) 中在控制器中给出直接检查的外观:
编辑:这是个坏主意,因为 view() 可能 return 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 已经对路径进行了检查,所以这个解决方案将添加第二次检查并增加服务器消耗..
最后我选择了这个不冷不热的解决方案:
我没有将它的函数 view() 扩展为 return false(并且必须通过 CI 然后在 ! 之后处理它),我只是创建了一个函数 is_view () 在 application/core/MY_Loader.php
我不确定 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>
我正在尝试根据加载的 third_party 路径检查视图文件是否确实存在
通常,我会检查视图是否存在 is_file(APPPATH.'/views/folder/'.$view)
我可以使用 get_package_paths 检索每个加载的 third_party 路径(感谢 Tpojka 的评论),然后检查它们的文件夹视图是否存在文件,
但我希望进行 'direct' 检查,就好像 ->view
函数会 return false
而不是重定向到错误页面
$html = $this->load->view($tpl,'',TRUE) ? $this->load->view($tpl,'',TRUE) : $another_template;
虽然我意识到可能没有其他解决方案可以通过加载路径添加此手动检查并将其隐藏在 CI_Load Class 扩展名 (application/core/MY_Loader) 中在控制器中给出直接检查的外观:
编辑:这是个坏主意,因为 view() 可能 return 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 已经对路径进行了检查,所以这个解决方案将添加第二次检查并增加服务器消耗..
最后我选择了这个不冷不热的解决方案:
我没有将它的函数 view() 扩展为 return false(并且必须通过 CI 然后在 ! 之后处理它),我只是创建了一个函数 is_view () 在 application/core/MY_Loader.php
我不确定 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>