在Modular Codeigniter中,一个模块的控制器如何/应该调用另一个模块的功能?

时间:2021-04-23 16:54:32

I am using CodeIgniter with the HMVC extension by wiredesignz.

我通过wiredesignz使用带有HMVC扩展的CodeIgniter。

My web app deals primarily with text articles. I have a module called articles. The model of the articles module, articles_model, contain functions that make database changes to the articles. For instance, the following will update the article:

我的网络应用主要处理文字文章。我有一个名为文章的模块。 articles模块的模型articles_model包含对文章进行数据库更改的函数。例如,以下内容将更新文章:

$this->articles_model->update_article();

There is another module for the user dashboard. Users will sometimes make updates to the articles from within their dashboard. The request is made from the dashboard controller, as this action is executed from views within the dashboard module.

用户仪表板还有另一个模块。用户有时会在其信息中心内对文章进行更新。请求来自仪表板控制器,因为此操作是从仪表板模块中的视图执行的。

In this situation, how should I let the dashboard controller communicate with the articles model? The couple of options I've come across so far is:

在这种情况下,我应该如何让仪表板控制器与文章模型进行通信?到目前为止我遇到的几个选项是:

  1. Make an update_article() function in the articles controller. All it does is call the articles_model function of the same name. Have the dashboard controller call the articles controller function update_article(). This would be done because I have read that in modular design, the controllers should be the point of communication between different modules (source). I've also come across other opinions that say controllers should never talk to each other. Hence my confusion.

    在文章控制器中创建update_article()函数。它所做的就是调用同名的articles_model函数。让仪表板控制器调用文章控制器函数update_article()。这样做是因为我已经读到,在模块化设计中,控制器应该是不同模块(源)之间的通信点。我也遇到过其他意见,说管制员不应该互相交谈。因此我的困惑。

  2. I could also duplicate the update_article() function in the dashboard_model. This way the dashboard controller will only have to call its own model, within its own module. My gut tells me this is not the way to go, as the update_article() function is called not only by the dashboard controller, but also quite a few others. This would result in a lot of duplicate code.

    我还可以在dashboard_model中复制update_article()函数。这样,仪表板控制器只需在自己的模块中调用自己的模型。我的直觉告诉我这不是要走的路,因为update_article()函数不仅被仪表板控制器调用,而且还被其他一些调用。这会导致很多重复的代码。

Any advice is appreciated.

任何建议表示赞赏。

1 个解决方案

#1


0  

You can load the model articles from module articles from another module Dashboard.

您可以从另一个模块仪表板中的模块文章加载模型文章。

Just load the model in the constructor function of the module (ex. Dashboard) you want the model to be available which should look like this.

只需在模块的构造函数(例如Dashboard)中加载模型,您希望模型可用,如下所示。

function __construct() {
  parent::__construct();

  ...

  // this is case sensitive, 1st part is the module, 2nd part is the class name
  $this->load->model('articles/Articles');
}

Then anywhere in your controller (ex. Dashboard), you can call $this->articles_model->update_article();.

然后在控制器的任何地方(例如仪表板),您可以调用$ this-> articles_model-> update_article();.

#1


0  

You can load the model articles from module articles from another module Dashboard.

您可以从另一个模块仪表板中的模块文章加载模型文章。

Just load the model in the constructor function of the module (ex. Dashboard) you want the model to be available which should look like this.

只需在模块的构造函数(例如Dashboard)中加载模型,您希望模型可用,如下所示。

function __construct() {
  parent::__construct();

  ...

  // this is case sensitive, 1st part is the module, 2nd part is the class name
  $this->load->model('articles/Articles');
}

Then anywhere in your controller (ex. Dashboard), you can call $this->articles_model->update_article();.

然后在控制器的任何地方(例如仪表板),您可以调用$ this-> articles_model-> update_article();.