Is it possible for one controller/method to call another controller/method? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Is it possible for one controller/method to call another controller/method? (/showthread.php?tid=62526) |
Is it possible for one controller/method to call another controller/method? - sneakyimp - 07-24-2015 Suppose I have a POST operation that submits a dozen input fields to a method in one controller, foo/original_post. Suppose in the process of trying to execute foo/original_post, some condition is detected that causes a problem and I want to pass all the submitted POST data to some method in another controller/method, say bar/some_other_method and have that other method display something entirely different based on the POST data. Is this possible? RE: Is it possible for one controller/method to call another controller/method? - Dracula - 07-24-2015 (07-24-2015, 03:46 PM)sneakyimp Wrote: Suppose I have a POST operation that submits a dozen input fields to a method in one controller, foo/original_post. If is the same controller you can do this: PHP Code: public function foo($data) For other controller...i really don't now RE: Is it possible for one controller/method to call another controller/method? - ivantcholakov - 07-24-2015 It is possible. The easiest method I can describe is when it happens within the same class. Code: class Bar extends CI_Controller { If the alternative method by some important reason is to be inside a different controller, then I can imagine using the modular extensions by wiredesignz. But I am lazy to write code for that, you can, I am sure. PS: Be careful how the second controller initializes. RE: Is it possible for one controller/method to call another controller/method? - Dracula - 07-24-2015 (07-24-2015, 05:02 PM)ivantcholakov Wrote: It is possible. The easiest method I can describe is when it happens within the same class. You better refactor your code beacause is not good. RE: Is it possible for one controller/method to call another controller/method? - sneakyimp - 07-25-2015 Thanks for your responses. I think I would agree that trying to call some other controller is going to be too ugly. I have altered my code -- this has resulted in some redundant code (essentially the same form handler in two different controllers) but I think it's a lot clearer and simpler than some convoluted way to invoke one controller from another. I'll try and move some of the form-handling logic into a model which should reduce the code redundancy. RE: Is it possible for one controller/method to call another controller/method? - Wouter60 - 07-25-2015 If you need a class that is shared between multiple controllers, the best method is to make a library out of it. In every controller where you need to access the shared functions/methods, you include the library with: PHP Code: $this->load->library('your_library_name'); |