简体   繁体   English

Codeigniter从外部应用程序文件夹加载视图?

[英]Codeigniter load view from outside application folder?

I'm trying to load a view from outside the application folder in Codeigniter (3.1.0) like this: 我正在尝试从Codeigniter(3.1.0)中的应用程序文件夹外部加载视图,如下所示:

public function index ( $page = '' ) {
    /**
     * Get active theme from DB
     */
    $active_theme = $this->m->getActiveTheme();

    /**
     * Change default view path
     */
    $this->_ci_view_paths = array(
        FCPATH . 'themes/' . $active_theme . '/' => TRUE
    );

    /**
     * Load index if page is not found
     */
    if ( ! file_exists( FCPATH . 'themes/' . $active_theme . '/' . $page . '.php')) {
        $this->load->view( 'index', $data );
    } else {
        $this->load->view( $page, $data );
    }
}

But I'm getting this error: 但我收到此错误:

Unable to load the requested file: index.php 无法加载请求的文件:index.php

Or whatever page I try to load. 或任何我尝试加载的页面。

Anyone know what I'm missing here? 有人知道我在这里想念的吗?

Ended up creating a custom loader: 最终创建了一个自定义加载器:

class MY_Loader extends CI_Loader {
    public function __construct() {
        $this->_ci_view_paths = array(FCPATH . 'themes/' => TRUE);
    }
}

I can then do: 然后,我可以这样做:

$this->load->view( $active_theme . '/index', $data );

On index.php in the latest version 在最新版本的index.php上

/*
 *---------------------------------------------------------------
 * VIEW DIRECTORY NAME
 *---------------------------------------------------------------
 *
 * If you want to move the view directory out of the application
 * directory, set the path to it here. The directory can be renamed
 * and relocated anywhere on your server. If blank, it will default
 * to the standard location inside your application directory.
 * If you do move this, use an absolute (full) server path.
 *
 * NO TRAILING SLASH!
 */
    $view_folder = '';

This is just an FYI. 仅供参考。

If you want the best of both worlds you can set your views to be in multiple places and CI will search them all... So adding to what Blaasvaer came up with you can set your new location and keep the existing one as well, if that is what you wanted to do... 如果您希望两全其美,则可以将视图设置在多个位置,CI会搜索所有视图...因此,添加到Blaasvaer提出的内容中,您可以设置新位置并保留现有位置,如果那就是你想做的...

class MY_Loader extends CI_Loader {
    public function __construct() {
    // append the new views path to the existing views path
       $this->_ci_view_paths = array( FCPATH . 'themes/' => true ) + $this->_ci_view_paths ;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM