Codeigniter 中模块实现的最佳技术

发布于 2024-12-22 18:15:16 字数 973 浏览 4 评论 0原文

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

为此,(我认为)我应该创建模块,但悲伤的部分 CI 默认情况下不接受任何模块。但通过 HMVC 或模块化 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

    }
}

上面的代码正是我想要得到的。可能有更好的方法来做到这些。不管怎样,我期待着您的回复......

I am a little familiar with CI, and now I am trying to build a web portal using it. I am trying to make it flexible enough to accept widgets/modules. Just like how components are to joomla.

For this, (I think) I should create modules, but the sad part CI by default does not accept any modules. But through HMVC or Modular CI, even this might be possible, but as i haven't used them before, I do not know which would fit in my case, for the long run.

Basically, I would like to communicate with other modules and their controller through a common controller. Kind of like a front controller.

For example take my default controller as Site and the functionality of what I am looking is somewhat like this...

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

    }
}

Above code is just what I am trying to get. There might be better way to do these. Either way I am looking forward to your replies.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一指流沙 2024-12-29 18:15:16

用户控制器

//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
}

User Controller

//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
} 
}

once inside your views you can access any number of modules...

//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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文