Codeigniter when to use redirect() and when to use $this->load->view Codeigniter when to use redirect() and when to use $this->load->view php php

Codeigniter when to use redirect() and when to use $this->load->view


I think you really answered your own question.

Use redirect() when a simple flash message at the top of another page is an appropriate response, use $this->load->view() when you're providing an entire page worth of feedback for whatever the incoming request may be.

So for example, when a new user signs up the "Success" page would be a loaded view and perhaps when a user edits something in their account a flash message "Changes saved" or soemthing similar on the same page is sufficient.


Redirect is also useful for two other common problems:

  • When a resource in you app is moved (and you want clients to remember the new URI)
  • After POSTing a form as one step in preventing back button rePOSTs


Your observation is correct that whenever you create some flashdata it is only available the time. That is because flashdata is just a special type of session which will available for your on the next request and after the next request it will automatically be deleted. You don't have to take care of its deletion.

This can be tested with the code:

$this->session->set_flashdata( 'test', 'testing' );echo $this->session->flashdata( 'test' );

Nothing will be printed. But now the next time execute the following code:

echo $this->session->flashdata( 'test' );

You will find the required output. Doing it one more time will not give any output. This is how they work. For details check the section Flashdata in http://codeigniter.com/user_guide/libraries/sessions.html

For the current page you don't require flashdata just pass the data to the view. Here is the code:

$data['test'] = 'testing';$this->load->view('sample_view', $data);

Bottom line is that use flashdata with redirect() and for views you should pass variables. Hope this helps!