Loading view outside view folder with CodeIgniter

后端 未结 7 2033
悲哀的现实
悲哀的现实 2020-12-03 12:44

I have the need to load a view from outside the scope of:

$this->load->view();

which appears to work from base/application/vie

7条回答
  •  攒了一身酷
    2020-12-03 13:11

    To customize models and views outside the 'application' folder, follow these easy steps,

    Create My_Loader.php file in 'application/core' directory Copy the code into the custom 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
            ));
    }
    

    Save the file and in the controller, call and load the model (which resides outside the application folder) as,

    $this->load->mymodel('folder/model');
    

    and for the view,

    $this->load->myview('views','view_dir/view-php-file', $data);
    

提交回复
热议问题