Thinkphp3.2 redirection and redirection for beginners

Source: Internet
Author: User

Thinkphp3.2 redirection and redirection for beginners

During application development, you will often encounter jump pages with prompts, such as pages with operation successes or operation errors, and automatically jump to another target page. The \ Think \ Controller class of the system has two built-in jump Methods success and error, which are used for page Jump prompts and support ajax submission. (Ajax submission is disabled by default. We will not discuss it here)

Find the three basic methods in \ Think \ Controller:

1 protected function error($message='',$jumpUrl='',$ajax=false) {2         $this->dispatchJump($message,0,$jumpUrl,$ajax);3     }
1 protected function success($message='',$jumpUrl='',$ajax=false) {2         $this->dispatchJump($message,1,$jumpUrl,$ajax);3     }

The difference between error and success is that the second parameter in the dispatchJump method, 0 is the error page, and 1 is the success page. continue to the next step and you will see the dispatchJump method.

1 if ($ status) {// message indicating successful sending 2 $ this-> assign ('message', $ message ); // prompt message 3 // after successful operation, the default value is 1 second 4 if (! Isset ($ this-> waitSecond) $ this-> assign ('waitsecond', '1'); 5 // if (! Isset ($ this-> jumpUrl) $ this-> assign ("jumpUrl", $ _ SERVER ["HTTP_REFERER"]); 7 $ this-> display (C ('tmpl _ ACTION_SUCCESS '); 8} else {9 $ this-> assign ('error', $ message ); // prompt 10 // The default value is 3 seconds 11 if (! Isset ($ this-> waitSecond) $ this-> assign ('waitsecond', '3'); 12 // if an error occurs by default, the system automatically returns 13 if (! Isset ($ this-> jumpUrl) $ this-> assign ('jumpurl', "javascript: history. back (-1); "); 14 $ this-> display (C ('tmpl _ ACTION_ERROR '); 15 // stop execution to avoid errors and continue to execute 16 exit; 17}

It is not hard to see that success and error are actually not much different from ordinary controllers. They are all output template files. The difference is that the correct and wrong judgments are made here. Of course, there is a small detail here, that is, after $ this-> success is executed, the subsequent code continues to be executed, and after $ this-> error is executed, it is disconnected here.

Next is the $ this-> redirect method. Remember, the $ this-> redirect and public methods redirect are different, $ this-> the url required by redirect is in the form of a module/controller/method, and the url of redirect is the url used in the address bar.

1 protected function redirect($url,$params=array(),$delay=0,$msg='') {2         $url    =   U($url,$params);3         redirect($url,$delay,$msg);4     }

$ This-> redirect calls the redirect method and uses the U method for url Processing before calling to make the two generic. For the U method, we will introduce it in detail later.

Note: URLs in $ this-> success and $ this-> error must be processed using the U method. URLs in $ this-> redirect cannot be processed using the U method.

The redirect method in functions. php is as follows:

1 function redirect ($ url, $ time = 0, $ msg = '') {2 // multi-Line URL supports 3 $ url = str_replace (array (" \ n ", "\ r"), '', $ url); 4 if (empty ($ msg )) 5 $ msg = "the system will automatically jump to {$ url} after {$ time} seconds }! "; 6 if (! Headers_sent () {7 // redirect 8 if (0 ===$ time) {9 header ('location :'. $ url); 10} else {11 header ("refresh: {$ time}; url = {$ url}"); 12 echo ($ msg ); 13} 14 exit (); 15} else {16 $ str = "<meta http-equiv = 'refresh' content = '{$ time }; URL = {$ url} '> "; 17 if ($ time! = 0) 18 $ str. = $ msg; 19 exit ($ str); 20} 21}

Note: The headers_sent () function checks whether the HTTP header has been sent and where it is sent. The headers_sent () function checks whether headers_sent () Redirects directly when the header is not sent. As can be seen from exit (): After $ this-> redirect is executed, subsequent code will not be executed. Whether to disconnect or not can be modified by yourself as needed (for example, if an email is sent after the submission is successful, the jump time will be affected if it is placed before the jump, and it will not be affected if it is placed after the jump ).

This article hopes to help the new thinkphp3.2 kids shoes. If something is wrong, you can propose that we will make common progress. If there is something promising, you can also send a private message to the blog site. Thank you for reading this article.

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.