A detailed explanation of the difference between thinkphp jump and redirect

Source: Internet
Author: User

Jump:

The browser considers that the current URL request is successful and the new URL is requested again.

The browser logs the current URL and the new URL in the request history.

Fallback, which can be rolled back to the current URL. (both success and error are the same)

Syntax implementation: At the browser level, modify the browser's location. href to achieve: location.href=href;

Redirect:

The browser considers that the current URL is not valid and is relocated to the new URL.

The browser does not record the current URL into the history,

Cannot be rolled back to the current URL.

The syntax implementation, all is the server sends the redirect response instruction to the browser,

Through the response header:

Header (' Location: '. URL), and immediately redirect to a URL

Header (' Refresh: ')

Example


The system's \think\controller class is built with two jump methods success and error, for page jump prompts, and can support Ajax submissions.
The use method is very simple, for example as follows:

$User = M (' User '); Instantiating the User object
$result = $User->add ($data);
if ($result) {
Set the address of the jump page after success, the default return page is $_server[' Http_referer ']
$this->success (' New success ', ' user/list ');
} else {
The default jump page for the error page is to return to the previous page, usually without setting
$this->error (' new failure ');
}
The first parameter of the success and error methods represents a hint, the second parameter represents a jump address, and the third parameter is a jump time (in seconds), for example:

Operation completed 3 seconds to jump to/article/index
$this->success (' Operation Complete ', '/article/index ', 3);
Operation failed 5 seconds before jumping to/article/error
$this->error (' Operation failure ', '/article/error ', 5);

The jump address is optional, and the default jump address for the success method is the $_server["Http_referer"],error method's default jump address is
Javascript:history.back (-1);.

The default Wait time success method is 1 seconds and the error method is 3 seconds

Success and Error methods can all correspond to the template, the default setting is two methods corresponding to the template are:

Default error jump corresponding template file
' Tmpl_action_error ' => think_path. ' Tpl/dispatch_jump.tpl ',
Default successful jump to corresponding template file
' Tmpl_action_success ' => think_path. ' Tpl/dispatch_jump.tpl ',
You can also use the template files inside the project
Default error jump corresponding template file
' Tmpl_action_error ' => ' public:error ';
Default successful jump to corresponding template file
' Tmpl_action_success ' => ' public:success ';

Template files can use template labels, and you can use the following template variables:

Variable meaning
$msgTitle Action Title
$message Page Tip Information
$status operation Status 1 indicates success 0 indicates failure and can be defined by the project itself.
$waitSecond Jump Wait Time unit is seconds
$JUMPURL Jump page Address
The success and Error methods automatically determine whether the current request belongs to an AJAX request and invoke the Ajaxreturn method return information If it is an AJAX request. Ajax methods below, the success and error methods encapsulate the following data back:

$data [' info '] = $message; Hint Message content
$data [' status '] = $status; State if it is success is 1 error is 0
$data [' url '] = $JUMPURL; Successful or incorrect jump address
redirect
The redirect method of controller class can realize the redirection function of the page.
The parameter usage of the redirect method is consistent with the use of the U function (reference URL generation section), for example:
Category action to redirect to new module

$this->redirect (' new/category ', Array (' cate_id ' => 2), 5, ' page jump ... ');

The above usage is to stay 5 seconds to jump to the new module category operation, and display the words in the page jump, redirect will change the current URL address.
If you just want to redirect to a specified URL address, rather than to a module's action method, you can use the redirect function redirect directly, for example:
Redirect to the specified URL address
Redirect ('/new/category/cate_id/2 ', 5, ' page jump ... ')
The first parameter of the redirect function is a URL address.
The difference between the redirect method of a controller and the redirect function is that the former is defined as a jump address with a URL rule, which is a pure URL address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.