CodeIgniter 本身就有MVC架構,但是如果當想要在不同的地方載入同樣的模組,這時候CodeIgniter HMVC 就是因應這種情況而產生的,在HMVC裡的Module裡有自己的controllers和views,完全不會跟原本的架構有互相衝突。
安裝:
1. 下載 hmvc 檔案
2. 解壓縮至 libraries 資料夾下
3. 修改 config/autoload.php 增加自動載入類別 $autoload['libraries'] = array(‘modules’);
4. 建立 modules 資料夾
增加模組: (以test為例)
1. 建立 modules/test/controllers 及 modules/test/views 目錄
2. 增加 modules/test/controllers/test.php 及 modules/test/views/test.php controller 跟view檔案
載入模組:
在輸出頁(view/welcome_message.php)要載入模組地方寫入
$this->modules->run(‘test’); or modules::run(‘test’);
說明:
$this->load->module(‘模組名/控制器/方法’);
以module test為例:
模組名:test
控制器:index(默認)
傳遞參數
$this->load->module(‘模組名/控制器/方法’,array(‘參數’,'參數’…));
回傳資料不輸出
$this->load->module(‘模組名/控制器/方法’,array(‘參數’,'參數’…),true);
參考資料:
- http://codeigniter.com/wiki/Modular_Extensions_-_HMVC/
- http://codeigniter.com/forums/viewthread/72123/P10/