2016-10-09 166 views
1

我试图从笨像这样的应用程序文件夹(3.1.0)外加载一个观点:Codeigniter从外部应用程序文件夹加载视图?

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

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

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

+0

为什么有文件在视图文件夹之外?为什么不包含它而不是加载它? – Brad

+0

将前端与后端分开。这样我可以将CI交换为任何其他后端。 – blaasvaer

回答

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); 
+0

当我说,我不是讽刺,如果你想要在各处放映你的观点,那么这是一个很好的解决方案。不知道为什么你会很高兴知道如果你愿意,很容易做到。我只是试了一下,它的功能很好。 – TimBrownlaw

+0

背后的原因是,我将前端与后端分开。因此,将我的前端放在CI特定的文件夹中不是一个选项。这意味着我可以将CI换成Laravel(重新创建控制器),使用AJAX处理数据相关的东西...... – blaasvaer

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 = ''; 
+0

这也不错。很好找! – TimBrownlaw

2

这仅仅是一个供参考。

如果您想要两全其美,您可以将视图设置在多个位置,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 ; 
    } 
} 
相关问题