Website-WeChat-backend error reporting mechanism [original]-php Tutorial

Source: Internet
Author: User
Website-back-end error reporting mechanism [original] based on the needs of project development, summarizes common error reminders for website development, including the error prompt page and the bounce after the login page is returned, both of them have simple methods for processing, but they both have obvious defects and call troubles. Therefore, after development and demonstration, I set up a back-end error reporting mechanism for a website project. At present, although the function is single, I think this is a good direction.

The following code is the source code written based on the Symfony framework:

Caller

Reference the method of the reported point, pass necessary parameters to enter the error handling process

/*** Exception handling mechanism ** @ param bool | false $ whether the default route is redirected to true flase false * @ param string $ message Error message ''automatically jumps to the logon page if it is null, the system will jump to the prompt page * @ param array $ data record the post parameters used to access this route. -- note that there is no file type or array type in the post prompt; otherwise, the callback is inevitable. report parameter loss (passing parameter _ POST) * @ param string $ url is empty by default. this parameter takes effect when defaulet is set to true, replace the configured default route * @ param int type with 1 by default. 1. return to the rest of the page. return to the route User's ajax request * @ return \ Symfony \ Component \ HttpFoundation \ RedirectResponse */ public function ex Ception_handling ($ default = false, $ message = '', $ data = array (), $ url ='', $ type = 1) {if ($ message! = '') {// The Force processing here is to prevent the program from getting stuck in an endless loop due to the loss of a parameter $ default = true ;} // encapsulate the parameter $ info = array ('data' => $ data,); if ($ default = true) {// jump to the default route if ($ url = '') {$ info ['URL'] = $ this-> get ('request ') -> getSchemeAndHttpHost (). $ this-> generateUrl ('wx _ INDEX'); // you need to reset and replace} else {$ info ['URL'] = $ url ;} $ info ['data'] = array ();} else {$ http_host = $ _ SERVER ['http _ host']; $ php_self = $ _ SERVER ['php _ SELF ']; $ query = $ _ SERVER ['query _ string']; $ info ['URL'] = 'http: // '. $ http_host. $ php_self; if ($ QUERY! = '') {$ Info ['URL']. = '? '. $ Query ;}// encapsulate the original value if (! Empty ($ data) {$ I = 0; foreach ($ data as $ index => $ value) {$ info ['info'] [$ I] ['key'] = $ index; $ info ['info'] [$ I] ['value'] = $ value; $ I ++ ;}} // serialize and package data into unset ($ info ['data']); $ info = urlencode (base64_encode (@ serialize ($ info ))); if ($ message = '') {// jump to the logon page if ($ type = 1) {return $ this-> redirect ($ this-> generateUrl ('wx _ auth_login ', array ('info' => $ info ))); // you need to reset the replace} else {return $ this-> get ('request')-> getSchemeAndHttpHost (). $ this-> generateUrl ('wx _ auth_login ', array ('info' => $ info )); // need to reset replace} else {// jump to the error page if ($ type = 1) {return $ this-> redirect ($ this-> generateUrl ('api _ wx_other_err ', array ('message' => $ message, 'info' => $ info); // you need to reset the replace} else {return $ this-> get ('request')-> getSchemeAndHttpHost (). $ this-> generateUrl ('api _ wx_other_err ', array ('message' => $ message, 'info' => $ info )); // you need to reset the replacement }}}

Acceptor processing method

1: Process and redirect the route parameter info. [this method is mainly used to log on to the page for redirect]

/*** @ Param $ info the error reporting mechanism is passed to encapsulate the parameter info * @ return string returned url */public function login_ OK _url ($ info) {if (! Is_array (@ unserialize (base64_decode (urldecode ($ info) {$ url = $ this-> get ('request')-> getSchemeAndHttpHost (). $ this-> generateUrl ('wx _ INDEX'); // you need to reset and replace the response page} else {$ url = $ this-> get ('request ') -> getSchemeAndHttpHost (). $ this-> generateUrl ('api _ wx_other_postJump ', array ('info' => $ info); // you need to reset the value to replace the error callback processor} return $ url ;}

2: Error message page jump logic

/*** Error message page ** @ param $ message * @ param $ info * @ return \ Symfony \ Component \ HttpFoundation \ Response */public function errAction ($ message, $ info) {// The received post parameter is used to submit the page again $ url = $ this-> get ('request')-> getSchemeAndHttpHost (). $ this-> generateUrl ('api _ wx_other_postJump ', array ('info' => $ info )); // the words above the hop-back button $ info = @ unserialize (base64_decode ($ info); if (! Is_array ($ info) | $ info ['URL'] = $ this-> get ('request')-> getSchemeAndHttpHost (). $ this-> generateUrl ('wx _ INDEX') {// you need to reconfigure $ url = $ this-> get ('request')-> getSchemeAndHttpHost (). $ this-> generateUrl ('wx _ INDEX'); $ text = ' ';} else {$ text =' ';} return $ this-> render ('@ Api/Default/wx_message.html.twig', array ('data' => array ('message' => $ message, 'URL' => $ url, 'text' => $ text )));}

Error callback processing controller (core method)-used to disassemble encapsulated parameters for corresponding request jump

/*** Internal post Request ** @ param $ info * @ return \ Symfony \ Component \ HttpFoundation \ RedirectResponse | Request */public function postJumpAction ($ info) {// Convert the array to $ info = @ unserialize (base64_decode (urldecode ($ info); if (! Is_array ($ info) {$ url = $ this-> get ('request')-> getSchemeAndHttpHost (). $ this-> generateUrl ('wx _ INDEX'); // reconfiguration} else {$ url = $ info ['URL'];} if (empty ($ info ['info']) {return $ this-> redirect ($ url );} else {return $ this-> render ('@ Api/Default/postJump.html. twig ', array ('data' => $ info); // reconfigure }}

Two important templates

1: postJump.html. twig template-the user sends a post request to the target route again

    
     Redirecting...

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.