• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
post data between controllers

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

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

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

#4
You want the form values passed from controller to controller?
Reply

#5
(12-01-2017, 07: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
Reply

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

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

#8
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.
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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