• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redirect & flashdata

[eluser]The Wizard[/eluser]
Hello friends,

when you've setup some flashdata and do a redirect()
the data gets lost. that is nasty and shouldnt happen Sad

take care Smile

I just worked on a site that uses this just the other day, and it seems to be working for me.

Are you using the same name/key for the flashdata in both places? E.g., does the controller code:

$this->session->set_flashdata('foo', 'your request was saved...');

match the view code that pulls out the value?


They have to have the same name. Finally, are you autoloading the session library, so it's available everywhere? If you're "manually" loading it, you might have forgotten it somewhere.

[eluser]The Wizard[/eluser]
have to double check that, will post when i do Smile thanks.

Im trying to do something similar, but I want to check, depending if the flashdata has been set, to redirect to a default page... or another page.

In other words, how do I check if the flashdata has been set?


I've been playing around with this and came up with a workaround that works fairly well for me.

What I've done is simulate a redirect through an automatic form submission:

First, create a view called "redirect_with_message.php" (codeigniter removes the script tags, so put them in when you create your file):
//script tag goes here
    function auto() {
//closing script tag goes here
<?php echo form_open($to,array('name'=>"myform")); ?>
    <input type="hidden" value="<?php echo $message; ?>" name="message">
Then create a helper entitled "redirect_helper.php" and place it in your helpers directory. I also added "redirect" to $autoload['helper']:
* Simulates a redirect, but sends message as well.
* @param  string $loc Codeigniter URI
* @param  string $message Message to be sent
* @param string $field Defaults to 'message', but you could change it to something else
* @return void
function redirect_msg($loc,$message="", $field="message") {

* Returns either the flashdata field or the posted field.
function showmsg($field='message') {
    return $CI->session->flashdata($field).$CI->input->post($field);

Now, if I want to send a message along with the redirect, I use
redirect_msg('path/to/controller/method',"This message was sent");
and in the view, I put
echo $showmsg;
and my message comes across whether or not I use the regular flashdata, or redirect_msg().

I load a model in one controller, then I save it in flashdata, then I redirect to another controller and I can't access that model there. If I try to access a method from that model I get the following error:

Fatal error: Call to a member function get_username() on a non-object in C:\wamp\www\CI172\dev\system\application\controllers\profile.php on line 28

How can I keep using the same model even if I redirect to another controller?


Create a MY_Controller and extend your other controllers from it.
You can Autoload the Model.

This is in the CodeIgniter User Guide.


What I am trying to do is reduce the work of loading the same model as I redirect() from one controller to another. I would like the same instance/state of my model exist in my second controller. The simplest approach I thought of is by placing it in the flashdata, but that didn't seem to work and produced the error I mentioned above. Are my only options to reload the model either through autoload or through MY_Controller class?

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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