Website – WeChat – Backend error mechanism "original"

Source: Internet
Author: User
Based on the requirements of the project development, summarizes the common exception reminders for Web site development including the error prompt page and return to the login page after the bounce problem, although both have a simple way to deal with, but there are obvious flaws and calls on the trouble. So after the development of the demonstration, I set up a website project of the backend error mechanism. At present although the function is single, but I think this is a good direction.

The following code is based on the Symfony framework write source

Call End

Error point reference method, pass the necessary parameters, enter the error processing process

/** * Exception Handling mechanism * * @param bool|false $default whether to jump the default route true True flase false * @param string $message Error message ' Auto jump to login page is not empty ' skip to the prompt page * @param array $data Log The parameters of the post that accesses this route for bounce back-note that the post prompt must not have a file type and an array type otherwise the callback will necessarily report the argument                Number lost (parameter _post) * @param string $url default is blank Defaulet set to True when this parameter takes effect, the user replaces the default route that has been set * @param int type The default is 1 1 for the return page the rest represents the return routing user Ajax request * @return \symfony\component\httpfoundation\redirectresponse */Public function exception_handling ($default = false, $message = ", $data = Array (), $url =", $type = 1) {if ($messa          GE! = ') {//Here is the mandatory handling, is to avoid the program because of a loss of a parameter into a dead loop $default = true;        }//Package parameter $info = array (' data ' = $data,); if ($default = = true) {//jump to the default route if ($url = = ") {$info [' url '] = $this->get (' re Quest ')->getschemeandhttphost ().           $this->generateurl (' Wx_index ');//need to reset replacement } 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;            }}//encapsulates the original value if (!empty ($data)) {$i = 0;                foreach ($data as $index = + $value) {$info [' info '] [$i] [' key '] = $index;                $info [' Info '] [$i] [' value '] = $value;            $i + +;        }}//Data serialization package unset ($info [' data ']);        $info = UrlEncode (Base64_encode (@serialize ($info)); if ($message = = ") {//Jump to login page if ($type = = 1) {return $this->redirect ($this-&gt    ; GenerateURL (' Wx_auth_login ', array (' info ' = $info)));//need to reset replacement} else {            return $this->get (' request ')->getschemeandhttphost (). $this->generateurl (' Wx_auth_login ', array (' info ' = = $info));//need to reset replacement}} else {//jump to Error page if ($type = = 1) {return $this->redirect ($this->generateurl (' Api_wx_other_err ', array (' message ' = $message, ' info ' = $info))); You need to reset the replacement} else {return $this->get (' request ')->getschemeandhttphost (). $this->gener Ateurl (' Api_wx_other_err ', array (' message ' = = $message, ' info ' = $info)); Need to reset replacement}}}

Receive-side processing mode

1: Handling and jumping methods for routing parameter Info "This method is primarily used to log on to pages that require a bounce back"

/**     * @param $info   error mechanism passed to encapsulate parameter info     * @return string   return URL     *    /Public Function Login_ok_url ($ Info)    {        if (!is_array (@unserialize (Base64_decode ($info)))) {            $url = $this->get (' request ') )->getschemeandhttphost (). $this->generateurl (' Wx_index ');//need to reset replace  default page        } else {             $url = $this->get (' request ') Getschemeandhttphost (). $this->generateurl (' Api_wx_other_postjump ', array (' info ' = = $info)); Need to reset replacement  error callback processor        }        return $url;    }

2: Error prompt page jump logic

/** * ERROR Prompt Page * * @param $message * @param $info * @return \symfony\com        Ponent\httpfoundation\response */Public Function erraction ($message, $info) {//Receive post parameters for page resubmit $url = $this->get (' request ')->getschemeandhttphost ().        $this->generateurl (' Api_wx_other_postjump ', array (' info ' = = $info));        The word $info = @unserialize (Base64_decode ($info)) above the bounce button; if (!is_array ($info) | | $info [' url '] = = $this->get (' request ')->getschemeandhttphost (). $this->generateurl (' Wx_index ') {//need to reconfigure $url = $this->get (' request ')->getschemeandhttphost (). $this->generateurl (' wx_in            Dex ');        $text = ' Return to home page ';        }else{$text = ' Return to previous page '; } return $this->render (' @Api/default/wx_message.html.twig ', array (' message ' = = $message, ' u    RL ' = $url, ' text ', ' = ' $text)]); }

Error callback processing controller (core method) – Used to disassemble encapsulated parameters for corresponding request jumps

/**     * Internal POST Request     *     * @param $info     * @return \symfony\component\httpfoundation\redirectresponse| Request     *    /Public Function postjumpaction ($info) {        //convert array        $info = @unserialize (Base64_decode ( UrlDecode ($info)));        if (!is_array ($info)) {            $url = $this->get (' request ')->getschemeandhttphost (). $this->generateurl (' Wx_ Index '); Reconfigure        }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 destination route again

    
 
      Jumping ...    

2: Hint template Wx_message.html.twig, according to the controller and the characteristics of their own project nested can be returned value

Data.url bounce          URL                    data.message      error message Data.text the         Word on the bounce button

If there are better suggestions please leave a message under the post, will actively listen to the

Permanent address of this article: http://blog.it985.com/16380.html

This article is from IT985 blog, please indicate the source and corresponding link when reproduced.

  • 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.