douzhuan0309 2013-06-28 06:27
浏览 40
已采纳

使用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 :-)

  • 写回答

7条回答 默认 最新

  • doutou3725 2013-06-28 09:04
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系