简体   繁体   中英

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:

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

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

/*
 *---------------------------------------------------------------
 * 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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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