What is the difference between the thinkphp U () and the redirect () and the success () Three jump methods??
Reply content:
What is the difference between the thinkphp U () and the redirect () and the success () Three jump methods??
The U helper function is to generate a URL that does not involve a jump function.
The difference between success and redirect is that the former has a smiley face pattern on the jump template, and no other difference.
You can see the source code of TP to find the answer.
The U function is used to generate the URL.
The public redirect function is used to redirect URLs.
The redirect method in the controller, also used to redirect the URL, you can specify the jump time, jump text.
The success method in the controller is the same as redirect, but there is a:) smiley face.
The error method in the controller is the same as redirect, but there is a: (The Crying face
If reading here is inconvenient, please visit the difference between the U function and the redirect, success methods in thinkphp.
Before I do, I want to correct your description, U this is not called method, called function.
As for the difference between the three, we can look at thinkphp the source code.
u function
/** * URL组装 支持不同URL模式 * @param string $url URL表达式,格式:'[模块/控制器/操作#锚点@域名]?参数1=值1&参数2=值2...' * @param string|array $vars 传入的参数,支持数组和字符串 * @param string|boolean $suffix 伪静态后缀,默认为true表示获取配置值 * @param boolean $domain 是否显示域名 * @return string */function U($url = '', $vars = '', $suffix = true, $domain = false){//省略}
In fact, his comments have been very clear, the return value is a string type, in fact, the return is the generated URL.
He's not an action, it's just an auxiliary function.
Success method
/** * 操作错误跳转的快捷方法 * @access protected * @param string $message 错误信息 * @param string $jumpUrl 页面跳转地址 * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 * @return void */ protected function error($message = '', $jumpUrl = '', $ajax = false) { $this->dispatchJump($message, 0, $jumpUrl, $ajax); } /** * 操作成功跳转的快捷方法 * @access protected * @param string $message 提示信息 * @param string $jumpUrl 页面跳转地址 * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间 * @return void */ protected function success($message = '', $jumpUrl = '', $ajax = false) { $this->dispatchJump($message, 1, $jumpUrl, $ajax); }
Here it is obvious to see, success and error both are encapsulated dispatchJump methods, the difference is the second parameter.
Let's go and have a dispatchJump look.
/** * Default Jump Action support error-oriented and correct jump * Call Template display default to public directory below Success page * Prompt page for configurable Support Template tag * @param string $message tip information * @param Boolean $status status * @param string $JUMPURL page Jump address * @param mixed $ajax ajax mode specify jump time when number * @ Access private * @return void */Private Function Dispatchjump ($message, $status = 1, $jumpUrl = ", $ajax = FA LSE) {if (true = = = $ajax | | Is_ajax) {//AJAX commit $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); }//Hint title $this->assign (' Msgtitle ', $status? L (' _operation_success_ '): L (' _operation_fail_ ')); If the close window is set, the window automatically closes if the prompt is finished ($tHis->get (' Closewin ')) {$this->assign (' Jumpurl ', ' javascript:window.close (); '); } $this->assign (' status ', $status); Status//Guaranteed output not affected by static cache C (' html_cache_on ', false); if ($status) {//Send success Information $this->assign (' message ', $message);//Prompt for 1 seconds by default after successful operation if (!isset ($this->waitsecond)) {$this->assign (' Waitsecond ', ' 1 '); }//The default action automatically returns the pre-action page if (!isset ($this->jumpurl)) {$this->assign ("Jumpurl", $_s erver["Http_referer"]); } $this->display (C (' tmpl_action_success ')); } else {$this->assign (' Error ', $message);/////Prompt for 3 seconds by default if error occurs (!isset ($this-> ; waitsecond) {$this->assign (' Waitsecond ', ' 3 '); }//Default error is automatically returned on page if (!isset ($this->jumpurl)) {$this->assign (' Jumpurl ', "JAV Ascript: History.back (-1); "); $this->display (C (' tmpl_action_error ')); Abort execution to prevent the execution of exit after an error; } }
We see that there is no code for jumping, just loading the template, registering several template variables and displaying them.
So where does it jump from? Clearly, it's on the template. Let's go to the default template to see:
(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);})();
Can be seen, is through javascript the location.href jump. That is, he is a client-implemented jump.
Redirect Method
/** * Action跳转(URL重定向) 支持指定模块和延时跳转 * @access protected * @param string $url 跳转的URL表达式 * @param array $params 其它URL参数 * @param integer $delay 延时跳转的时间 单位为秒 * @param string $msg 跳转提示信息 * @return void */ protected function redirect($url, $params = array(), $delay = 0, $msg = '') { $url = U($url, $params); redirect($url, $delay, $msg); }
We can see that the redirect method first uses the U function to obtain the address to do the parameter, passes to the redirect function. Is the encapsulation of this function.
Let's take a redirect look at the function again.
/** * URL重定向 * @param string $url 重定向的URL地址 * @param integer $time 重定向的等待时间(秒) * @param string $msg 重定向前的提示信息 * @return void */function redirect($url, $time = 0, $msg = ''){ //多行URL地址支持 $url = str_replace(array("\n", "\r"), '', $url); if (empty($msg)) { $msg = "系统将在{$time}秒之后自动跳转到{$url}!"; } 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); }}
Can see a very short function, there are two kinds of service-side jump mode, respectively, whether the HTTP header has been output the situation. is the service-side jump.
Summarize
UThe function will only be used to generate a URL and will not perform a jump.
successThe method itself does not jump, but because the default template has a jump javascript code.
redirectThe method is to invoke the service-side function to make a jump.
See Thinkphp's official documentation