Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the views default path on Codeigniter 3.0 versions

On codeigniter 3 when loading a view, the default path is application / views /

I would like to be able to change the default path to application / views / template /

I use to be able to change the default view path on MY_Loader.php $this->_ci_view_path = APPPATH .'views/template/'; as shown below. Currenly it only suited for CI2 does not seem to work on CI3

Question On codeigniter 3.0 versions what is the best method of changing the default view path, can it be done similar to my MY_Loader from CI2 to 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");
   }
}

1 Answers

Solved

The problem was here

CI2 Versions

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

Now CI3 Versions

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

How to change the default view path in 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");

    }
}

Update for HMVC & Codeigniter 3

This symbol * in glob on code below meaning module name.

<?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");

    }
}

This allows you to do this for HMVC loading of views

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

Instead of

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