CodeIgniter Forums
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.

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?

If is the same controller you can do this:

PHP Code:
public function foo($data)
{
 
  if ($data['example'] == 'no-example') {
 
     $this->bar($data);
 
  } else {
 
     //do something else
 
  }
}

private function 
bar($data)
{
 
  //do something with the 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 {

    public _construct {
        ...
    }

    public function original_post {

        $post = $this->input->post();

        if (!empty($post['condition'])) { // Some condition for switching to an alternative method.

            $this->alternative_post();

            return; // Don't forget this.
        }

        $data = array(/*...*/); // Other data.

        $this->load->view('original_post_view', $data); // $_POST data is accessible in the view through form_helper functions set_value(), etc.
    }

    function alternative_post() { // Or named as _alternative_post if we don't want this method to be accessed bi the browser.

        //$post = $this->input->post(); // If this is needed.

        $data = array(/*...*/);

        $this->load->view('alternative_post_view', $data);
    }

}

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.


Code:
class Bar extends CI_Controller {

   public _construct {
       ...
   }

   public function original_post {

       $post = $this->input->post();

       if (!empty($post['condition'])) { // Some condition for switching to an alternative method.

           $this->alternative_post();

           return; // Don't forget this.
       }

       $data = array(/*...*/); // Other data.

       $this->load->view('original_post_view', $data); // $_POST data is accessible in the view through form_helper functions set_value(), etc.
   }

   function alternative_post() { // Or named as _alternative_post if we don't want this method to be accessed bi the browser.

       //$post = $this->input->post(); // If this is needed.

       $data = array(/*...*/);

       $this->load->view('alternative_post_view', $data);
   }

}


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');