CodeIgniter Forums
post data between controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: post data between controllers (/showthread.php?tid=69478)



post data between controllers - yinkoh - 12-01-2017

Hello,

Is there a way to pass the post data between 2 controllers ?

I've search on google and forums and the answer seems to be no, althought using the flash_data session could provide a bypass solution.

If I've missed a way to achieve this, I'll be glad that someone explain it Smile

Thanks


RE: post data between controllers - PaulD - 12-01-2017

Personally, I think it is very bad practice to have one controller call another (unless you are using wiredesignz HMVC, which I have always found to be overkill in truth).

The idea is that a request is passed to a controller. The controller deals with the request by calling models for data, or libraries for business logic, or whatever. It then calls the views for the response.

The only real exception I can think of is if you do a redirect. With a redirect the controller is really saying that this request needs to be dealt with by another controller. If your controller method has arguments, data can be passed via those arguments. An alternative but IMHO messier way is to load the data into a session variable (perhaps as a flash-data) and let the other controller read the session data.

Here is a silly example of what I mean:

Login contoller
PHP Code:
function login()
{
 
// Does authorisation check
 // finds user account has been banned
 
redirect('users/banned/'.$user_code);


Users contoller
PHP Code:
function banned($user_code)
{
 
// Get user details
 // get reasons user banned
$this->load->view('users/banned_view'$data);


Here the user id is passed from the login controller to the users controller showing the banned message.

But, the login controller would NOT call the banned controller to get data. As I said, bit of a silly example really, as you would not do this particular task in this way.

Hope that helps,

Paul.


RE: post data between controllers - yinkoh - 12-01-2017

Thanks for your answer.
What I meant is not quite really that.

From the main controller a form allows the user to edit search settings.
Then when submitted, a result controller is called.
Within the result controller, the submit button recalls the same controller to display results according to any choice the user make.
Everything is fine at that point (I mean search controls are refills accordingly to their previous values).

My problem comes when the user clicks on a displayed result which calls another controller containing the same search form.
I hoped that post data would have been transmitted to the controller and thus edit controls but no.

I hope I made myself clearer.


RE: post data between controllers - Martin7483 - 12-01-2017

You want the form values passed from controller to controller?


RE: post data between controllers - yinkoh - 12-01-2017

(12-01-2017, 06:59 AM)Martin7483 Wrote: You want the form values passed from controller to controller?

Yep, what i'm actually doing is adding form values as uri like ?price=20000 and then filtering get data to repopulate the form.

Kind of ugly so i'd like a better way to do it


RE: post data between controllers - Wouter60 - 12-01-2017

I would use a session variable (array) to achieve this.
Once you've repopulated the form, you can unset the session variable. This is way more flexible than passing values by GET, and users can't manipulate the url.


RE: post data between controllers - yinkoh - 12-01-2017

Ok, thanks, I'll forward to it this week-end


RE: post data between controllers - InsiteFX - 12-03-2017

Or add an array to your MY_Controller then all controllers can have access to it.

Or you can pass the values to the controller method.