codeigniter - How do I call a function inside another controller file in PHP?
I have one function I need to call from another file with Codeigniter and I'm not sure how to do this.
The one file is in controllers/Jobassignment.php, which contains a function called createassignments() that I need to call from controllers/Reporting.php. I am pretty new at Codeigniter, so how would I call the createassignments function from Reporting.php?
I tried
$this->load->library('controllers/jobassignment');
$this->jobassignment->createassignments();
But this isn't working.
It has been called in a form tag:
<form action="<?php echo base_url( 'jobassignment/createassignments' ); ?>
but I can't do that in the middle of a function.
They are both controller files. Any help would be appreciated.
Answer
Solution:
It has nothing to do with Codeigniter. If the Jobassignment.php is a PHP class you can make an instance of it and then call that function:
but if this method you create is going to call in other classes you must make a class under Helpers directory and then call the method the way that I explained.
Don't make instance of request class. you must make a helper class to handel this.
Answer
Solution:
If the goal is to make the action of the form a method in a different controller then your code
is perfectly fine. You just have to understand that you are doing a redirect and code execution in the current controller is done. Understand that a
<form action...>
is always a redirect where the input element values of the form are sent to the server.Because it is a redirect there is no reason to call
$this->load->library('controllers/jobassignment');