0

I have two controller and one model like below, I want to call second controller method in side model. But i am not getting how to call it.

1) Controller1

   class controller1 extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->load->model('job');
    }

    public function getjob() {
        $this->job->check_payment();
    }

}

2) Paypal.php (controller)

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Paypal extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->load->model('job');

        // Load helpers
        $this->load->helper('url');

        // Load PayPal library
        $this->config->load('paypal');

        $config = array(
            'Sandbox' => $this->config->item('Sandbox'),
            'APIUsername' => $this->config->item('APIUsername'),
            'APIPassword' => $this->config->item('APIPassword'),
            'APISignature' => $this->config->item('APISignature'),
            'APISubject' => '',
            'APIVersion' => $this->config->item('APIVersion')
        );

        // Show Errors
        if ($config['Sandbox']) {
            error_reporting(E_ALL);
            ini_set('display_errors', '1');
        }

        $this->load->library('Paypal_pro', $config);
    }

    function sendPayemnt() {
        echo "Hello...";
    }

}

2) Job.php (model)

class Job extends CI_Model {

    function check_payment() {
        // I want to call method of Paypal controller here...
    }

}

I hope someone help me to solve it. Thanks,

3
  • 1
    Why you want to code out of Codeigniter's structure? Commented Oct 6, 2017 at 9:40
  • 2
    Write a helper function instead of controller and call it from model. This is the correct structure for Codeigniter. Commented Oct 6, 2017 at 9:42
  • I know this is not valid coding structure, but this is only way to achieve my requirements. Commented Oct 6, 2017 at 9:42

1 Answer 1

2

Although it is technically possible, if you think that you need to, it suggests a flaw in your application's design.

The Controller layer is the backbone of you application and meant to handle requests from the user, talk to the Model layer, and stitch together the output in the View. Your Model layer should be blind to the Controller and View, but deal with data manipulation only. This is an over-simplified explanation of the MVC pattern (you can find resources for that elsewhere).

You can use like this:

class controller1 extends CI_Controller{

public function testsample(){
        $this->load->model('modal1');
        $stations=$this->modal1->getController();
        echo "<pre>"; print_r($stations);exit;
    }

    public function getData(){
        $ta=array(0=>'Sample',1=>'test');
        return $ta;
    }
}

modal:

class modal1 extends CI_Model {
   function getController()
   {
        $controllerInstance = & get_instance();
        $controllerData = $controllerInstance->getData();
        return $controllerData;
   }
}

output:

Array
(
    [0] => Sample test cases
    [1] => test
)

i will access my controller action as controller1/testsample you can also use curl to call the controller action

Sign up to request clarification or add additional context in comments.

3 Comments

Hello, sorry but i am new to codeigniter. So dont know how to use your answer in my case? Can you please explain a bit more. Thanks,
@Hardik : i have updated the example, check it now. i hope it is clear now. If it solves your issue then accept the answer and upvote.
thanks for your answer and update. Sorry but i have updated my question. If possible can you please check it. i appreciate your help. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.