Welcome Guest, Not a member yet? Register   Sign In
Redirect & flashdata
#1

[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
#2

[eluser]t'mo[/eluser]
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:

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

match the view code that pulls out the value?

Code:
<?=$this->session->flashdata('foo');?>

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

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

[eluser]yayot[/eluser]
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?

Thanks
#5

[eluser]jloosli[/eluser]
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):
Code:
//script tag goes here
    function auto() {
        document.myform.submit();
    }
//closing script tag goes here
<body>
<?php echo form_open($to,array('name'=>"myform")); ?>
    <input type="hidden" value="<?php echo $message; ?>" name="message">
</form>
</body>
Then create a helper entitled "redirect_helper.php" and place it in your helpers directory. I also added "redirect" to $autoload['helper']:
Code:
<?php
/**
* 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") {
    $CI=&get;_instance();
    $data=array(
        "to"=>$loc,
        $field=>$message
    );
    $CI->load->view('redirect_with_message',$data);
}

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

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

[eluser]Dushan[/eluser]
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?

Thanks!
Dushan
#7

[eluser]InsiteFX[/eluser]
Create a MY_Controller and extend your other controllers from it.
or
You can Autoload the Model.

This is in the CodeIgniter User Guide.

InsiteFX
#8

[eluser]Dushan[/eluser]
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?




Theme © iAndrew 2016 - Forum software by © MyBB