What are the differences between U () and redirect () and success () in ThinkPHP ??

Source: Internet
Author: User
What are the differences between U () and redirect () and success () in ThinkPHP ?? What are the differences between U () and redirect () and success () in ThinkPHP ??

Reply content:

What are the differences between U () and redirect () and success () in ThinkPHP ??

The u helper function generates a url and does not involve the jump function.
The difference between success and redirect is that the former has a smiley face on the jump template, and there is no difference between the other.
You can check the source code of tp to find the answer.

The U function is used to generate a URL.
Public redirect functions are used to redirect URLs.
The redirect Method in the controller is also used to redirect URLs. you can specify the jump time and jump text.
The success method in the controller is the same as The redirect Method, but there is a :) smile.
The error method in the controller is the same as The redirect Method, but there is a (crying face)

If reading is inconvenient, visit the difference between the U function and the redirect and success methods in ThinkPHP.

Before that, I want to correct your description,UThis is not called a method, but a function.
Let's take a look at the differences between the three.thinkphpSource code.

U function
/*** URL Assembly supports different URL modes * @ param string $ url expressions. Format: '[module/controller/Operation # anchor @ domain name]? Parameter 1 = value 1 & parameter 2 = value 2... '* @ param string | array $ vars input parameters. arrays and strings * @ param string are supported. | boolean $ suffix pseudo-static suffix, the default value is true. * @ param boolean $ domain indicates whether the domain name is displayed * @ return string */function U ($ url = '', $ vars = '', $ suffix = true, $ domain = false) {// omitted}

In fact, his comment has already been quite clear. the returned value is a string type, but actually the generated URL is returned.
He is not an action, but an auxiliary function.

Success method
/*** Shortcut for redirecting operation errors * @ access protected * @ param string $ message Error message * @ param string $ jumpUrl page jump address * @ param mixed $ ajax is Ajax method: specify the jump time * @ return void */protected function error ($ message = '', $ jumpUrl = '', $ ajax = false) {$ this-> dispatchJump ($ message, 0, $ jumpUrl, $ ajax );} /*** shortcut for successful operation Jump * @ access protected * @ param string $ message prompt message * @ param string $ jumpUrl page jump address * @ param mixed $ ajax is Ajax when using a number, specify the jump time * @ return void */protected function success ($ message = '', $ jumpUrl = '', $ ajax = false) {$ this-> dispatchJump ($ message, 1, $ jumpUrl, $ ajax );}

Obviously,successAnderrorAll are encapsulated.dispatchJumpMETHOD. The difference is the second parameter.
Let's go againdispatchJumpLook.

/*** Default redirect operation supports error orientation and correct redirection * display the success page under the default public directory in the call Template * prompt page: configurable support template tag * @ param string $ message prompt information * @ param Boolean $ status * @ param string $ jumpUrl page jump address * @ param mixed $ ajax specifies the jump time when using a number * @ access private * @ return void */private function dispatchJump ($ message, $ status = 1, $ jumpUrl = '', $ ajax = false) {if (true ===$ ajax | IS_AJAX) {// AJAX submit $ data = is_array ($ ajax )? $ Ajax: array (); $ data ['info'] = $ message; $ data ['status'] = $ status; $ data ['URL'] = $ jumpUrl; $ this-> ajaxReturn ($ data);} if (is_int ($ ajax) {$ this-> assign ('waitsecond', $ ajax);} if (! Empty ($ jumpUrl) {$ this-> assign ('jumpurl', $ jumpUrl);} // The Title $ this-> assign ('msgtitle', $ status? L ('_ OPERATION_SUCCESS _'): L ('_ OPERATION_FAIL _'); // if the window is closed, the window is automatically closed after the prompt is complete. if ($ this-> get ('closewin') {$ this-> assign ('jumpurl', 'javascript: window. close (); ') ;}$ this-> assign ('status', $ status ); // status // ensure that the output is not affected by the static cache C ('HTML _ CACHE_ON ', false); if ($ status) {// successful message $ this-> assign ('message', $ message); // prompt message // after successful operation, the default value is 1 second if (! Isset ($ this-> waitSecond) {$ this-> assign ('waitsecond', '1');} // if (! Isset ($ this-> jumpUrl) {$ this-> assign ("jumpUrl", $ _ SERVER ["HTTP_REFERER"]);} $ this-> display (C ('tmpl _ ACTION_SUCCESS ');} else {$ this-> assign ('error', $ message ); // prompt message // 3 seconds by default if (! Isset ($ this-> waitSecond) {$ this-> assign ('waitsecond', '3');} // if an error occurs by default, the system automatically returns if (! Isset ($ this-> jumpUrl) {$ this-> assign ('jumpurl', "javascript: history. back (-1); ") ;}$ this-> display (C ('tmpl _ ACTION_ERROR '); // Stop Execution to avoid an error and continue execution exit ;}}

We can see that there is no jump code here, just loading the template, registering several Template variables and displaying them.
So where does it jump? Obviously, it is on the template. Let's look at the default template:

(function(){var wait = document.getElementById('wait'),href = document.getElementById('href').href;var interval = setInterval(function(){    var time = --wait.innerHTML;    if(time <= 0) {        location.href = href;        clearInterval(interval);    };}, 1000);})();

You can see thatjavascriptOflocation.href. That is, it is the jump implemented by the client.

Redirect Method
/*** Jump to Action (URL redirection) you can specify a module or delay jump * @ access protected * @ param string $ url jump URL expression * @ param array $ params other URL parameters * @ param integer $ delay jump time unit. for seconds * @ param string $ msg jump prompt * @ return void */protected function redirect ($ url, $ params = array (), $ delay = 0, $ msg = '') {$ url = U ($ url, $ params); redirect ($ url, $ delay, $ msg );}

We can see that,redirectFirst, useUThe function obtains the address as a parameter and passes itredirectFunction. Is the encapsulation of this function.
Let's go againredirectFunction.

/*** URL redirection ** @ param string $ url-redirected URL address * @ param integer $ time the waiting time for the redirection (seconds) * @ param string $ prompt information before msg redirection * @ return void */function redirect ($ url, $ time = 0, $ msg = '') {// multi-line URL supports $ url = str_replace (array ("\ n", "\ r"), '', $ url ); if (empty ($ msg) {$ msg = "The system will automatically jump to {$ url} After {$ time} seconds }! ";} If (! Headers_sent () {// redirect if (0 ===$ time) {header ('Location :'. $ url) ;}else {header ("refresh: {$ time}; url ={$ url}"); echo ($ msg) ;}exit ();} else {$ str ="
  "; If (0! = $ Time) {$ str. = $ msg;} exit ($ str );}}

You can see that there are two server jump methods for a very short function to determine whether the http header has been output. Is the server jump.

Summary

UThe function is only used to generate a url and does not perform a jump.
successThe method itself does not jump, but because the default template has a jumpjavascriptCode.
redirectThe method is to call the server function to jump.

Read thinkPHP official documentation

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.