CodeIgniter Forums
Return to differnt views - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Return to differnt views (/showthread.php?tid=19787)



Return to differnt views - El Forum - 06-18-2009

[eluser]robert.fulcher[/eluser]
I want to have links to edit the same thing from multiple areas in my site. When they are done I want them to return to the area that they started from. I wanted to get some input on the best way to track the referring url in CI.

I thought that I would pass the destination controller in the uri and parse it out for the redirect in the edit controller.

Any other better ideas.


Return to differnt views - El Forum - 06-18-2009

[eluser]TheFuzzy0ne[/eluser]
Code:
$this->session->set_flashdata('last_url' => $this->uri->uri_string());

I think that would work nicely, you'd just need to have it run on every request, so you may need to auto load it as a self-initialising library, or extend the controller class.

./system/application/libraries/MY_Controller.php
Code:
class MY_Controller extends Controller
{
    function MY_Controller()
    {
        parent::Controller();
        $this->session->set_flashdata('last_url' => $this->uri->uri_string());
    }
}

To access the last URL, you can do something like this:
Code:
$last_url = $this->session->flashdata('last_url');



Return to differnt views - El Forum - 06-18-2009

[eluser]Dam1an[/eluser]
Fuzz, 2 things
1) Seeing as it's being executed on every request, why not put it in as normal userdata? That way you save garbage collectiong flash data
2) That will get executed at the start of EVERY request, including the one redirecting to the form, you the last_url on the form page would be the URL of the form, would it not?

Maybe write the URI to the session in a hook (not sure which ones you have to choose from that are are available before the session is written)


Return to differnt views - El Forum - 06-18-2009

[eluser]TheFuzzy0ne[/eluser]
Yes, but you'd have grabbed the URL when the form was initially loaded, and perhaps added that as flashdata:
Code:
if ($this->session->flashdata('return_url'))
{
    $return_url = $this->session->flashdata('return_url');
}
else
{
    $return_url = $this->session->flashdata('last_url');
    $this->session->set_flashdata('return_url', $return_url);

}

Personally, I think flash data is ideal.


Return to differnt views - El Forum - 06-18-2009

[eluser]Dam1an[/eluser]
Another possible option would be the whence library. I've never used it personally, but might be what you're after. It can also track the last x pages, so you can send them back any number of pages


Return to differnt views - El Forum - 06-18-2009

[eluser]robert.fulcher[/eluser]
The Whence library is almost what I am looking for.

I am not sure where to set the flash variable at...on the view before the call to the controller?


Return to differnt views - El Forum - 06-18-2009

[eluser]robert.fulcher[/eluser]
Ok so this is what I have come up with so far. It seems to work fine but I am still new to CI so I would like to make sure I am not doing something stupid....

Code:
$numsegments = $this->uri->total_segments()- 4;
$target = '';
$segmentstart = 4;
while ($numsegments <> 0) {
   $target = $target.$this->uri->segment($segmentstart + 1).'/';
   $segmentstart++;
   $numsegments--;
};

To explain. This is usually for an edit scenario. In the URI I know where the ID is for the item I will edit. So that ID will be where I start $segmentstart. I have added the destination or $target to the end of the URI like below...

Code:
&lt;?php echo anchor('todo/todo_edit_c/index/'.$todo->id.'/todo/todo_c', 'EDIT');?&gt;

So it works fine but I wonder if there is another and if it would be good to put this into a Help - library - pi not sure it would qualify for.

Thanks for all the great help out there.