• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it possible for one controller/method to call another controller/method?

#1
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?
Reply

#2
(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
Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
Reply

#3
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.
Reply

#4
(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.
Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
Reply

#5
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.
Reply

#6
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'); 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.