codeigniter - How do I call a function inside another controller file in PHP?

755

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.

532

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:

user namespace/Jobassignment

$jobassignment = new Jobassignment();

$jobassignment->createassignments();

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.

569

Answer

Solution:

If the goal is to make the action of the form a method in a different controller then your code

<form action="<?php echo base_url( 'jobassignment/createassignments' ); ?>

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

People are also looking for solutions to the problem: php - Get input type="text" values by checked checkboxes

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.