CodeIgniter Forums
The right way to redirect - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: The right way to redirect (/thread-29660.html)



The right way to redirect - El Forum - 04-16-2010

[eluser]zoonix[/eluser]
Hey all, I'm still having lots of fun learning about CI and I'm hoping there are a couple more kinds souls that might share some pointers with me.

All I'm trying to figure out is how to handle things after calling a function. In my test code below I have tried two ways of loading the view once the function is done.

In delete_file I tried $this->index which works except the list of files still shows the deleted file until you refresh.

In the upload function I tried using redirect('dev/test', 'refresh') which works fine by bringing me back to the index and refreshes the file list nicely, but I lose the data in $data['message'].

I'm sure that I'm making this much more difficult than need be. Any suggestions? What is the recommended way of doing this?

Thanks!!

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends Controller {
    
    function Test() {
        parent::Controller();
        $this->load->helper(array('download', 'file'));
        $this->data['files'] = $this->list_files();
        
        if(!array_key_exists('message',$this->data)){
            $this->data['message']='crap';
        }
    }
    
    function index() {
        $this->load->view('template', $this->data);
    }
    
    // Delete file
    function delete_file($file) {
        unlink('./upload/test/'.$file);
        
        $this->data['message'] = $file.' deleted';

        $this->index();
    }
    
    // List files
    function list_files() {
        $dir = './upload/test';
        
        if($dh = opendir($dir)) {
            while(($file = readdir($dh)) !== false) {
                if ($file != "." && $file != "..") {
                    $files[] = $file;
                }
            }
            closedir($dh);
            if(!isset($files)) {
                $files[] = 'none';
            }
        }
        return $files;
    }
    
    // Upload file
    function upload() {
        $config['upload_path'] = './upload/test/';
        $config['allowed_types'] = 'csv';
        
        $this->load->library('upload', $config);
    
        if ( ! $this->upload->do_upload()) {
            $this->data['message'] = $this->upload->display_errors();
        }    
        else {
            $data['data'] = array('upload_data' => $this->upload->data());
            $this->data['message'] = 'Successfully uploaded '.$data['data']['upload_data']['orig_name'];
        }
        
        redirect('dev/test', 'refresh');
    }
}



The right way to redirect - El Forum - 04-16-2010

[eluser]connors[/eluser]
I'd use
Code:
redirect(site_url('dev/test'), 'refresh');
to ensure the path relative to your index.php (You'll need the url helper for the site_url() function)

To get the message to another page, use flash data.
Code:
$this->session->set_flashdata('upload_message', 'Successfully uploaded '.$data['data']['upload_data']['orig_name']);
While you on your dev/test page retrives it:
Code:
if(isset($this->session->flashdata('upload_message')) echo $this->session->flashdata('upload_message');
See the session library for documentation


The right way to redirect - El Forum - 04-16-2010

[eluser]cahva[/eluser]
[quote author="connors" date="1271464638"]I'd use
Code:
redirect(site_url('dev/test'), 'refresh');
to ensure the path relative to your index.php (You'll need the url helper for the site_url() function)
[/quote]
Sorry but you're wrong about this. redirect function already uses site_url function so you dont have to add it anymore. So just redirect('dev/test') will do fine.

But with flashdata I agree 100% Smile Thats the way to inform something's changed. Action -> create flashdata -> redirect -> show flashdata


The right way to redirect - El Forum - 04-16-2010

[eluser]zoonix[/eluser]
Thanks guys! This that's exactly what I was looking for. It always seems so obvious when you get the answer.


The right way to redirect - El Forum - 04-16-2010

[eluser]connors[/eluser]
[quote author="cahva" date="1271468049"]
Sorry but you're wrong about this. redirect function already uses site_url function so you dont have to add it anymore. So just redirect('dev/test') will do fine.
[/quote]
Thanks for the correction Wink


The right way to redirect - El Forum - 04-19-2010

[eluser]zoonix[/eluser]
Thanks again for the help. Here's what I came up based of the suggestions. Pretty basic, but maybe it'll help some other newb like me.

Controller...
Code:
// Delete file
function delete_file($file) {
    unlink('./upload/test/'.$file);
        
    // Set the status message and redirect to index
    $this->session->set_flashdata('message', $file.' deleted');
    redirect('dev/test', 'refresh');
}

View...
Code:
<p>&lt;?php
    if($this->session->flashdata('message') != '' ) {
        echo $this->session->flashdata('message');
    }
    else {
        echo 'No Message';
    }
?&gt;</p>



The right way to redirect - El Forum - 04-19-2010

[eluser]n0xie[/eluser]
You could also try my message library to take care of sending status messages to an user.