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

时间:2016-10-09 22:45:00

标签: php codeigniter

我试图从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 );
    }
}

但是我收到了这个错误:

无法加载请求的文件:index.php

或者我尝试加载的任何页面。

任何人都知道我在这里失踪了吗?

3 个答案:

答案 0 :(得分:2)

结束创建自定义加载器:

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

我可以这样做:

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

答案 1 :(得分:2)

在最新版本的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 = '';

答案 2 :(得分:1)

这只是一个FYI。

如果你想要两全其美,你可以将你的观点设置在多个地方,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 ;
    }
}