CodeIgniter HMVC

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/controllersmodules/test/views 目錄
2. 增加 modules/test/controllers/test.phpmodules/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);

參考資料:

Related Posts with Thumbnails

相關文章

發表迴響

您的電子郵件位址並不會被公開。 必要欄位標記為 *

*

您可以使用這些 HTML 標籤與屬性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>