2016-10-09 162 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 ; 
    } 
} 
相關問題