【问题标题】:Codeigniter load view from outside application folder?Codeigniter 从外部应用程序文件夹加载视图?
【发布时间】:2016-10-09 22:45:00
【问题描述】:

我正在尝试从 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

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

有人知道我在这里缺少什么吗?

【问题讨论】:

  • 为什么文件在视图文件夹之外?为什么不包含它而不是加载它?
  • 前端与后端分离。这样我就可以将 CI 换成任何其他后端。

标签: php codeigniter


【解决方案1】:

最终创建了一个自定义加载器:

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

我可以这样做:

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

【讨论】:

  • 我并不是在讽刺,如果您想在各处发表意见,这是一个很好的解决方案。不知道您为什么会这样做,但很高兴知道如果您愿意,这很容易做到。我刚试了一下,效果很好。
  • 这背后的原因是,我将前端与后端分开。因此,将我的前端放在 CI 特定文件夹中不是一种选择。这意味着我可以将 CI 换成 Laravel(当然是重新创建控制器),将 AJAX 用于数据相关的东西......
【解决方案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 = '';

【讨论】:

    【解决方案3】:

    这只是一个仅供参考。

    如果您想要两全其美,您可以将视图设置在多个位置,CI 会搜索它们...好吧,如果那是你想做的……

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

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      相关资源
      最近更新 更多