It takes me longer time when I found out what is the difference between redirect() a $this->load->view() behavior in CodeIgniter. So, I’ve decided to share my knowledge. Why to discover hot water? 😉
The questions I had:
- Why couldn’t I see my data in view? I.e. Why aren’t shown the data I set?
- Why anybody use the redirect once and the load view in other cases? When use which one?
The basic rule = the change of context:
- in case of change use redirect (e.g. switch to new item in menu or in data array)
- in case of NO change use load->view (e.g. re-submit of the form after input error)
How to handle data exchange:
In case of redirect you have basically 2 options:
- use URL parameters
redirect('client/get_client/' . $client->family_id . '/' . $client_id);
- use set_flashdata
$this->session->set_flashdata('message', $this->auth->messages()); redirect(base_url(), 'refresh');
Warning: The method set_flashdata serves to send the information to the next context i.e. not to the current one, this is why NOT possible to get its current value WITHOUT change of URL, resp. without refresh of the current one.
In case of load view you have 1 option:
- use standard passing of data via parameters
Warning: If you want to “pipeline” more views, i.e. in one view do loading of another one (e.g. header), be aware the array $data will already be split into its sub-items in the 1st view. So, you CANNOT pass them ($data) into another view directly.
That’s it 😉