开发者

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

开发者 https://www.devze.com 2023-01-27 04:53 出处:网络
I am fairly new to Codeigniter and I am wondering on some Codeigniter best practices. When should i use redirect() versus using

I am fairly new to Codeigniter and I am wondering on some Codeigniter best practices. When should i use redirect() versus using

$this->load->view

It seems that when I use redirect() then $this->session->set_flashdata works like it should but when i开发者_如何学Python use

$this->load->view

the message is displayed after an additional request.


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!


it's pretty simple. what url do you want the user to be on? if they are on url1 and post data back to url1 and you just load a different view, they will still be on url1. if you redirect to url2, they will go to url2.


You need to use PRG - Post/Redirect/Get pattern. Redirect and load view aren't the same if you have the form in the content of the page.

Scenario:

There is a view, view_1 with form in it to debit money from a account. After submission of the form in the view_1 you want to jump to view_2 with a success message and you have 2 options to achieve the same. 1. load view_2 with success message or 2. redirect to view_2 with flash data carrying success message.

Option 1: load view_2 with success message When you submit the form and refresh, it will cause resubmission and cause multiple debit from the account, which shouldn't be the case. You too can see the alert popping od "Confirmation of form resubmission".

Option 2: This is the right answer PRG

PRG - Post/Redirect/Get PRG is a web development design pattern that prevents some duplicate form submissions which means, Submit form (view_1) -> Redirect -> Get (view_2)

Under the hood

Redirect status code - HTTP 1.0 with HTTP 302 or HTTP 1.1 with HTTP 303

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

The redirect status code is to ensure that in this situation, the web user's browser can safely refresh the server response without causing the initial HTTP POST request to be resubmitted.

Source

Double Submit Problem

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

Post/Redirect/Get Solution

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

0

精彩评论

暂无评论...
验证码 换一张
取 消