我需要从以下范围之外加载视图:
$this->load->view();
这似乎可以从base/application/views
目录中工作。如何从/application/
目录外部访问视图?
我想我将不得不延长CI_Loader class
这是最好的前进方式吗?
我还找到了保存view_paths的数组:
// base/system/core/Loader.php
// CI_Loader
/**
* List of paths to load views from
*
* @var array
* @access protected
*/
protected $_ci_view_paths = array();
但是所有声明变量之上的评论让我陷入困境
// All these are set automatically. Don't mess with them.
任何关于从这里去哪里的想法将不胜感激:-(
不知道这是否是正确的方法,但它:)
在您的application/core
文件夹中放置此加载程序扩展
<?php
class MY_Loader extends CI_Loader {
function ext_view($folder, $view, $vars = array(), $return = FALSE) {
$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH . $folder . '/' => TRUE));
return $this->_ci_load(array(
'_ci_view' => $view,
'_ci_vars' => $this->_ci_object_to_array($vars),
'_ci_return' => $return
));
}
}
?>
然后你想要一个外部视图文件,假设它在third_party文件夹中
应用/third_party/my_new_view.php
Hello : <?php echo $my_name; ?>
然后在控制器中调用新视图
ext_view是新的视图加载器方法,
- 第一个参数:你申请的文件夹
- 第二个参数:视图名称
- 第三个参数:变量数据等...
test_controller.php
$view_data = array('my_name' => 'dino');
$this->load->ext_view('third_party', 'my_new_view', $view_data);
如果一切正常。 它将输出
你好:恐龙
Dino's
解决方案似乎非常合理,并且确实适用于application
中的文件,但是,我需要更深入的解决方案。我们的 CI 嵌入到主目录的子目录中,类似于 ip.ip.ip.ip/dir/sub/application/..
.也许我做错了什么,但我无法让他的解决方案完全满足我的需求,即使在尝试应用类似 ../../
的东西之后,这仍然不可行;如果我需要更深入地了解未知的后目录计数怎么办?
因此,我最终编写了自己的解决方案,到目前为止,它似乎工作得很好,因为它使用了香草 PHP。以这种方式,它试图获取您的文件所在的基本目录并加载它们,这与 CI 的加载函数的工作方式几乎相同。
与他的解决方案相同,在名为 MY_Loader.php
的application/core
中创建一个名为 的文件。然后只需写下以下内容(或者如果您也想保留他的解决方案,只需添加该方法(:
<?php
class MY_Loader extends CI_Loader {
public function base_view($view, $vars = array(), $get = FALSE) {
// ensures leading /
if ($view[0] != '/') $view = '/' . $view;
// ensures extension
$view .= ((strpos($view, ".", strlen($view)-5) === FALSE) ? '.php' : '');
// replaces ''s with /'s
$view = str_replace('''', '/', $view);
if (!is_file($view)) if (is_file($_SERVER['DOCUMENT_ROOT'].$view)) $view = ($_SERVER['DOCUMENT_ROOT'].$view);
if (is_file($view)) {
if (!empty($vars)) extract($vars);
ob_start();
include($view);
$return = ob_get_clean();
if (!$get) echo($return);
return $return;
}
return show_404($view);
}
}
然后,如果你在最内部的根目录中有一个名为'bob.php'的文件,只需调用:
$this->load->base_view('bob');
// OR
$this->load->base_view('bob.php');
// OR if it's extension is .html
$this->load->base_view('bob.html');
如果它位于基本根目录的另一个目录中:
$this->load->base_view('directory/bob');
// OR
$this->load->base_view('directory'bob.htm');
或者随心所欲,只要您在真实目录中调用一个真实文件!
希望这个替代解决方案可以帮助有类似危险的人。
若要自定义'application'
文件夹之外的模型和视图,请按照以下简单步骤操作,
在目录中创建My_Loader.php
'application/core'
文件将代码复制到自定义My_Loader.php
class MY_Loader extends CI_Loader {
function mymodel($model, $folder = '',$vars = array(), $return = FALSE) {
array_push($this->_ci_model_paths, ""); //replace "" with any other directory
parent::model($model);
}
function myview($folder, $view, $vars = array(), $return = FALSE) {
$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH . '../' . $folder . '/' => TRUE));
return $this->_ci_load(array(
'_ci_view' => $view,
'_ci_vars' => $this->_ci_object_to_array($vars),
'_ci_return' => $return
));
}
保存文件并在控制器中,调用模型并将其加载为:
$this->load->mymodel('folder/model');
而对于视图,
$this->load->myview('views','view_dir/view-php-file', $data);
在应用程序/核心/中创建文件MY_Loader.php
<?php
class MY_Loader extends CI_Loader {
function __construct() {
parent::__construct();
$CI =& get_instance();
$CI->load = $this;
}
public function ext_view($view, $vars = array(), $return = FALSE){
$_ci_ext = pathinfo($view, PATHINFO_EXTENSION);
$view = ($_ci_ext == '') ? $view.'.php' : $view;
return $this->_ci_load(array(
'_ci_vars' => $this->_ci_object_to_array($vars),
'_ci_path' => $view, '_ci_return' => $return));
}
}
在应用程序/核心/中创建文件MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load =& load_class('Loader', 'core', 'MY_');
$this->load->initialize();
log_message('debug', "Controller Class Initialized");
}
}
使用 MY_Controller
而不是 ext_view CI_Controller
$this->load->ext_view(path/to/the/view/file,@param2,@param3);
前任:
class Welcome extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->ext_view('/path/to/view');
}
}
遵循 Dino 的回答并更正ci_vars,作为视图 Codeigniter 的函数:
点火器查看功能
public function view($view, $vars = array(), $return = FALSE)
{
return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return));
}
Dino的视图功能现在将是:
function ext_view($folder, $view, $vars = array(), $return = FALSE) {
$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH . $folder . '/' => TRUE));
return $this->_ci_load(array(
'_ci_view' => $view,
'_ci_vars' => $this->_ci_prepare_view_vars($vars),
'_ci_return' => $return
));
}
这是我在 CodeIgniter 论坛上找到的一个更直接的答案 https://forum.codeigniter.com/thread-58412.html
这是我对docmattman提出的解决方案的轻微偏差:
class MY_Loader extends CI_Loader{
public function __construct()
{
parent::__construct();
}
public function load_ext_view($view, $vars = array(), $return = FALSE)
{
$view_file = '/full/path/to/'.$view.'.php';
if(file_exists($view_file)){
$view_to_load = array('_ci_path' => $view_file,
'_ci_view' => $view,
'_ci_vars' => $this->_ci_object_to_array($vars),
'_ci_return' => $return
);
return $this->_ci_load($view_to_load);
}
return $this->view($view, $vars, $return);
}
}