在 Codeigniter 3.0 版本上更改视图默认路径

Changing the views default path on Codeigniter 3.0 versions

在 codeigniter 3 上加载视图时,默认路径是 application/views/

我希望能够将默认路径更改为 application/views/template/

我曾经能够更改 MY_Loader.php $this->_ci_view_path = APPPATH .'views/template/'; 上的默认视图路径,如下所示。目前它只适用于 CI2 似乎不适用于 CI3

问题 在 codeigniter 3.0 版本上更改默认视图路径的最佳方法是什么,是否可以像我的 MY_Loader 从 CI2 到 CI3 那样完成。

<?php

class MY_Loader extends CI_Loader {

   function __construct() {
      // Change this property to match your new path

      $this->_ci_view_path = APPPATH .'views/template/';
      $this->_ci_ob_level  = ob_get_level();
      $this->_ci_library_paths = array(APPPATH, BASEPATH);
      $this->_ci_helper_paths = array(APPPATH, BASEPATH);
      $this->_ci_model_paths = array(APPPATH);
      log_message('debug', "Loader Class Initialized");
   }
}

实际上这不是您问题的答案,但它可以作为替代解决方案。

重写 view() 方法或通过扩展 CI_Loader 创建新函数以获得所需结果

public function my_view($file,$data)
{
    $this->view("template/$file",$data);
}

如果您再次检查 index.php 文件,您会发现每一行都得到了很好的解释。对于主题中的这一特定行,写为:

If you do move this, use the full server path to this folder.

已解决

问题出在这里

CI2 版本

$this->_ci_view_path = APPPATH .'views/somefoldername/';

现在 CI3 版本

$this->_ci_view_paths = array(
    APPPATH . 'views/somefoldername/' => TRUE
);

如何在 Codeigniter 3 中更改默认视图路径

application > core > MY_Loader.php

<?php

class MY_Loader extends CI_Loader {

    public function __construct() {

        $this->_ci_ob_level  = ob_get_level();

        $this->_ci_view_paths = array(
            APPPATH . 'views/somefoldername/' => TRUE
        );

        $this->_ci_library_paths = array(APPPATH, BASEPATH);

        $this->_ci_model_paths = array(APPPATH);

        $this->_ci_helper_paths = array(APPPATH, BASEPATH);

        log_message('debug', "Loader Class Initialized");

    }
}

HMVC 和 Codeigniter 3 更新

下面代码中的这个符号 * 表示模块名称。

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the MX_Loader class */
require APPPATH."third_party/MX/Loader.php";

class MY_Loader extends MX_Loader {

    public function __construct() {

        $this->_ci_ob_level  = ob_get_level();

        // Default
        $this->_ci_view_paths = array(
            APPPATH . 'views/' => TRUE
        );

        // Modules
        $module_view_paths = glob(APPPATH . 'modules/*/views/template/', GLOB_ONLYDIR);

        foreach ($module_view_paths as $module_view_path) {
            $this->_ci_view_paths = array(
                $module_view_path => TRUE,
            );
        }

        $this->_ci_library_paths = array(APPPATH, BASEPATH);

        $this->_ci_model_paths = array(APPPATH);

        $this->_ci_helper_paths = array(APPPATH, BASEPATH);

        log_message('debug', "Loader Class Initialized");

    }
}

这允许您为视图的 HMVC 加载执行此操作

$this->load->view('folder_name/view_name');

而不是

$this->load->view('template/folder_name/view_name');

将此更改为

$this->_ci_view_path = APPPATH .'views/template/'; # Works on CI 3.0-

这个

$this->_ci_view_paths = array(APPPATH.'views/template/' => TRUE); # Works on CI 3.0+