Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

php - Loading view outside view folder with CodeIgniter

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

$this->load->view();

which appears to work from base/application/views directory. How can I access a view from outside the /application/ directory ?

I assume i will have to extend the CI_Loader class would this be the best way forward ?

I have also found the array which holds the 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();

but the comment above all the declared variables has got me stuck

// All these are set automatically. Don't mess with them.

Any ideas on where to go from here would be greatly appreciated :-)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't know whether it's a correct way, but it works :)

In your application/core folder put this loader extention

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

}

?>

Then you want a external view file, suppose its in the third_party folder

application/third_party/my_new_view.php

Hello : <?php echo $my_name; ?>

Then call your new view in the controller

ext_view is your new view loader method,

  • 1st param : the folder inside you applicaton
  • 2nd param : the view name
  • 3rd param : the variables data and so on...

test_controller.php

$view_data = array('my_name' => 'dino');
$this->load->ext_view('third_party', 'my_new_view', $view_data);

If everything fine. it will output

Hello : dino


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...