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

I'm new to Codeigniter, and I'm curious about some of the best Codeigniter methods. When to use redirect() versus using

 $this->load->view 

It seems that when I use redirect() , then $this->session->set_flashdata works as it should, but when I use

 $this->load->view 

A message appears after an additional request.

+7
php codeigniter
source share
5 answers

I think you really answered your question.

Use redirect () when a simple flash message at the top of another page is the appropriate answer, use $ this-> load-> view () when you provide full feedback value for any incoming request maybe.

So, for example, when a new user signs the Success page, it will be a loaded view, and perhaps when the user edits something in his account, the flash message “Changes Saved” or “Mapping” on the same page is enough.

+6
source share

Redirecting is also useful for two other common problems:

  • When moving a resource in your application (and you want clients to remember the new URI)
  • After posting a form as a single step in preventing the rePOSTs back button
+4
source share

Your observation is true that whenever you create some flashdata , only time is available. This is because flashdata is a special type of session that will be available for your next request, and after the next request it will be automatically deleted. You do not need to worry about deleting it.

This can be verified using the code:

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

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

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

You will find the desired result. Doing this again will not give any result. This is how they work. For more information, see Flashdata strong> at http://codeigniter.com/user_guide/libraries/sessions.html

For the current page, you do not need flashdata just pass data to the view. Here is the code:

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

The bottom line uses flashdata with redirect() , and for views you must pass variables. Hope this helps!

+4
source share

It is pretty simple. what url do you want the user to be enabled on? if they are on url1 and send the data back to url1 and you just load another view, they will still be on url1. if you redirect url2, they will go to url2.

+1
source share

You need to use the PRG template - Post / Redirect / Get. Redirecting and viewing the load do not match if you have a form in the content of the page.

 Scenario: 

There is a view, view_1 with a form in it for debit money from an account. After submitting the form in view_1, you want to go to view_2 with a successful message, and you have 2 options to achieve the same. 1. load view_2 with a success message, or 2. redirect to view_2 with a message about the successful execution of flash data.

Option 1: load view_2 with a successful message When you submit a form and update it, it causes a resubmission and causes a multiple debit from the account, which should not be. You can also see a message about the warning “Confirm re-submit form”.

Option 2: This is the correct PRG answer

PRG - message / forwarding / receiving PRG is a web development design template that prevents some duplicate forms, which means Send form (view_1) → Redirect → Get (view_2)

 Under the hood 

The redirection status code is HTTP 1.0 with HTTP 302 or HTTP 1.1 with HTTP 303

An HTTP response with a redirect status code will additionally provide a URL in the location header field. The user agent (for example, a web browser) is prompted with an answer with this code to make a second, otherwise identical request to the new URL specified in the location field.

The redirection status code is designed to ensure that in this situation, the web user's browser can safely update the server response without causing the original HTTP POST request to be resent.

A source

 Double Submit Problem 

Duplicate issue

 Post/Redirect/Get Solution 

Post / Redirect / Get Solution

+1
source share

All Articles