CodeIgniter Forums
communication between controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: communication between controllers (/showthread.php?tid=68932)



communication between controllers - normeno - 09-14-2017

Hi!

I have an input controller (api) and it has to communicate with different controllers as needed, for example, users, products. Currently one controller calls another through CURL, what is the best way to communicate with each other?

The project is in Codeigniter 3

Best Regards


RE: communication between controllers - dmyers - 09-14-2017

normeno,  normally you want to make your controllers pretty light (ie on actual source code/biz logic) and then create a library for the heavy lifting. That way any controller can talk to those libraries in any other controller.

You can have
Code:
$user = $this->user->get_current();
and
Code:
$this->product->get_by_id($product_id);


Also depending on what you are trying to do when working with data object's the model might be the best "shared" place for your code.
Code:
$product = $this->product_model->by_id($product_id);


Hope this helps.

Then you don't need to call controllers from controllers...

DMyers


RE: communication between controllers - spjonez - 09-19-2017

A request should route through a single controller. In MVC controllers should not cross talk.