2011-12-27 39 views
0

我對CI有點熟悉,現在我正在嘗試構建一個使用它的Web門戶。我試圖讓它足夠靈活來接受小部件/模塊。就像組件如何joomla。爲此,(我認爲)我應該創建模塊,但是可悲的部分CI默認情況下不接受任何模塊。但通過HMVC或模塊化CI,即使這可能是可能的,但由於我以前沒有使用過它們,所以從長遠來看,我不知道哪種方法適合我的情況。在Codeignitor中實現模塊的最佳技術

基本上,我想通過一個通用控制器與其他模塊及其控制器進行通信。有點像前端控制器。

例如把我的默認控制器網站和我所期待的功能會受到一定是這樣的...

class Site extends CI_Controller { 
    public function index() { 
     $appName = $this -> uri -> segment(1); // Take this as app name 
     $appControllerName = $this -> uri -> segment(2); // Take this as app controller name 


     $this -> load -> module($appName); //Loading our app Module 

     $this -> $appName -> load -> controller($appControllerName); 

     $this -> $appName -> $appControllerName -> render(); 
     // Take Render() as one of the common method that all the modules controller should have and is reponsible for rendering the HTML 

    } 
} 

上面的代碼正是我試圖讓。有可能有更好的方法來做到這一點。無論哪種方式,我很期待您的答覆.....

回答

1

用戶控制器

//MX_Controller is the HMVC controller, so anything extending 
//this class is a Module 

class User extends MX_Controller{ 

//Public function hidden from URL but accessed via Module 

public function _comments($user_id){ 
    //grab comments for this users from your database 
    //return as an array or object 
} 
} 

你的觀點裏一旦你可以訪問任意數量的模塊...

//Dashboard_view.php 

//Module One 
foreach(Modules::run('User/_comments', $user_id) as $user_comments) 
{ 
    // return all comments for this user 
} 

//Module Two 
foreach(Modules::run('Widgets/_show_random_stuff', $user_id) as $user_widgets) 
{ 
    // return all widgets for this user 
} 
+0

我很困惑..... – joomlearner

+0

我更新了我的答案,以更好地格式化代碼 – Philip

+0

所以你建議,我應該使用.. HMVC模塊... – joomlearner