Is it possible for one controller/method to call another controller/method? |
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? (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
Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
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. (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.
Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
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.
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'); |
Welcome Guest, Not a member yet? Register Sign In |