【问题标题】:Best technique for module implementation in CodeigniterCodeigniter 中模块实现的最佳技术
【发布时间】:2011-12-27 05:45:07
【问题描述】:

我对 CI 有点熟悉,现在我正在尝试使用它构建一个 Web 门户。我正在尝试使其足够灵活以接受小部件/模块。就像组件对 joomla 的作用一样。

为此,(我认为)我应该创建模块,但可悲的部分 CI 默认情况下不接受任何模块。但是通过 HMVC 或 Modular CI,即使这也是可能的,但由于我以前没有使用过它们,所以从长远来看,我不知道哪种适合我的情况。

基本上,我想通过一个通用控制器与其他模块及其控制器进行通信。有点像前端控制器。

例如,将我的默认控制器作为站点,我正在寻找的功能有点像这样......

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

    }
}

上面的代码正是我想要得到的。可能有更好的方法来做这些。无论哪种方式,我都期待您的回复.....

【问题讨论】:

    标签: codeigniter module codeigniter-2 hmvc


    【解决方案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
    }
    

    【讨论】:

    • 我会采用这种方法是的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 2011-04-28
    • 2017-05-24
    • 2013-07-06
    相关资源
    最近更新 更多