Switch between mobile phone and PC browser with ThinkPHP3.2.2 frame

Source: Internet
Author: User

thinkphp Build Mobile Website:

Http://www.php100.com/html/php/lei/2013/0904/1699.html

This article (certainly not the base 3.2.2) is a way to determine the app's path at the frame entrance by judging the phone or PC access, so that my phone web and PC can't share the MVC three layer.

--------------------------------------------------------------------------------------------------------------- --

The following is found in thinkphp official website, is also the solution provided by netizens, there are two kinds ofMethod One

One, TP 3 . 2 adds IsMobile () to {/application/common/function. php },tp 3 . 1 Add to {Project/common/common. PHP }

Function ismobile ()  {    //  If there is http_x_wap_profile, it must be a mobile device      if  (isset  ($_server[' http_x_wap_profile '))          return true;        //This article is excerpted from the TPM Smart Switch template engine for TPM development      if (isset  ($_server[' http_client ")  && ' phoneclient ' ==$_server[' http_client '])          return true;    //if the VIA message contains a WAP, it must be a mobile device, Some service associations block this information     if  (isset  ($_server[' Http_via '))          //is not found for flase, otherwise true        return stristr ($_ server[' Http_via '],  ' WAP ')  ? true : false;    //determine the client flag sent by the phone, Compatibility needs to be improved     if  (isset  ($_server[' http_user_agent '))  {          $clienTkeywords = array (             ' Nokia ', ' Sony ', ' Ericsson ', ' mot ', ' Samsung ', ' HTC ', ' SGH ', ' lg ', ' sharp ', ' sie-', ' Philips ', ' Panasonic ', ' Alcatel ', ' Lenovo ', ' iphone ', ' ipod ', ' blackberry ', ' Meizu ', ' Android ', ' NetFront ', ' Symbian ', ' UCWeb ', ' windowsce ', ' palm ', ' Operamini ', ' Operamobi ', ' Openwave ', ' nexusone ', ' cldc ', ' MIDP ', ' wap ', ' Mobile '          '         //from http_user_agent to find the keywords in the mobile browser          if  (Preg_match ("/("  . implode (' | ',  $clientkeywords)  .  ")/I",  strtolower ($_server[' http_user_agent ')))  {             return true;        }    }     //protocol law, because it may not be accurate, put to the final Judgment     if  (isset  ($_server[' http_ ACCEPT ']))  {        //  If only WML is supported and HTML is not supported it must be a mobile device          //  if WML and HTML are supported but WML is before HTML mobile device         if  (( Strpos ($_server[' http_accept '],  ' vnd.wap.wml ')  !== false)  &&  (Strpos ($_server [' Http_accept '],  ' text/html ')  === false | |   (Strpos ($_server[' http_accept '],  ' vnd.wap.wml ')  < strpos ($_server[' http_accept '),   ' text/html ')))  {            return  true;        }    }     return false; }

Second, TP3. 2 in {Application/common/controller/frontendcontroller.class. PHP}, add the following code (Ps:frontendcontroller is my front-end controller, Basecontroller is my base controller)

Class Frontendcontroller extends Basecontroller {public Function _initialize () {//Mobile device browsing, toggle template if (IsMobile (        ) {//Set default default theme for mobile C (' Default_theme ', ' mobile '); }        //............ More code for you ...}}

TP3. 1 in {Project/lib/commonaction. php}, or add the following code to your public controller

Class Commonaction extends action{public Function _initialize () {//Mobile device browsing, toggle template if (IsMobile ()) {        Set default default theme for mobile C (' Default_theme ', ' mobile '); }        //............ Your more code .....} }

Third, the other controllers are inherited Frontendcontroller (TP3. 2) or commonaction (TP3. 1)
If there is no other special template configuration, create a new mobile folder in the Application/home/view folder, and put it in a well-written version index.html file, set up chrome for phone UA, open the home page, this will automatically display the mobile version of the home page template. :)

Method Two

Through JS judgment, jump to the mobile domain name.

<script type= "Text/javascript" > (function (A, b) {if (/(Android|bb\d+|meego). +mobile|avantgo|bada\/| Blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip (Hone|od) |iris|kindle|lge |maemo|midp|mmp| Netfront|opera m (ob|in) I|palm ( os)? | Phone|p (Ixi|re) \/|plucker|pocket|psp|series (4|6) 0|symbian|treo|up\. (Browser|link) |vodafone|wap|windows  (Ce|phone) |xda|xiino/i.test (a) | | /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac (er|oo|s\-) |ai (KO|RN) |al (Av|ca|co) |amoi|an (ex|ny |YW) |aptu|ar (Ch|go) |as (te|us) |attw|au (di|\-m|r |s ) |avan|be (CK|LL|NQ) |bi (lb|rd) |bl (Ac|az) |br (e|v) w| bumb|bw\-(n|u) |c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co (mp|nd) |craw|da (it|ll|ng) |dbte|dc\-s|devi|dica| Dmob|do (c|p) o|ds (12|\-d) |el (49|ai) |em (L2|ul) |er (ic|k0) |esl8|ez ([4-7]0|os|wa|ze] |fetc|fly (\-|_) |g1 u|g560| Gene|gf\-5|g\-mo|go (\.w|od) |gr (Ad|un) |haie|hcit|hd\-(m|p|t) |hei\-|hi (Pt|ta) |hp (&NBSP;I|IP) |hs\-c|ht (C (\-|  |_|a|g|p|s|t) |TP) |hu (AW|TC) |i\-(20|go|ma) |i230|iac ( |\-|\/) |ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja (t|v) a|jbro|jemu|jigs|kddi|keji|kgt ( |\/) |klon|kpt  |kwc\-|kyo (c|k) |le (no|xi) |lg ( g|\/(k|l|u) |50|54|\-[a-w]) |libw|lynx|m1\-w|m3ga|m50\/|ma (TE|UI|XO) |MC (01|21|CA) |m\-cr|me (Rc|ri) |mi (o8|oa|ts) |mmef|mo (01|02|bi|de|do|t (\-| |o|v) |zz) |mt (50|p1|v ) |mwbp| Mywa|n10[0-2]|n20[2-3]|n30 (0|2) |n50 (0|2|5) |n7 (0 (0|1) |10) |ne ((c|m) \-|on|tf|wf|wg|wt) |nok (6|i) |nzph|o2im|op (ti| WV) |oran|owg1|p800|pan (a|d|t) |PDXG|PG (13|\-([1-8]|c)) |PHIL|PIRE|PL (AY|UC) |pn\-2|po (ck|rt|se) |prox|psio|pt\-g| QA\-A|QC (07|12|21|32|60|\-[2-7]|i\-) |qtek|r380|r600|raks|rim9|ro (Ve|zo) |s55\/|sa (Ge|ma|mm|ms|ny|va) |SC (01|h\-| oo|p\-) |sdk\/|se (c (\-|0|1) |47|mc|nd|ri) |sgh\-|shar|sie (\-|m) |SK\-0|SL (45|id) |sm (AL|AR|B3|IT|T5) |so (ft|ny) |sp ( 01|h\-|v\-|v ) |sy (01|MB) |t2 (18|50) |t6 (00|10|18) |ta (gt|lk) |tcl\-|tdg\-|tel (i|m) |tim\-|t\-mo|to (pl|sh) |ts ( 70|M\-|M3|M5) |tx\-9|up (\.b|g1|si) |utst|v400|v750|veri|vi (rg|te) |vk (40|5[0-3]|\-v) |VM40|VODA|VULC|VX (52|53|60| 61|70|80|81|83|85|98) |w3c (\-| ) |webc|whit|wi (G&NBSP;|NC|NW) |wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test (A.substr (0,4))) WINDOW.LOCATION=B}) (Navigator.useragent| | navigator.vendor| | Window.opera, ' HTTP//fill in your WAP URL ');</script>



Finally, my plan

The destructive nature of my plan will affect the future of the framework upgrade, which is basically to add the above IsMobile method to/thinkphp/common/functions.php, and then in/thinkphp/library/ View.class.php 167 Use IsMobile This method to judge, is the mobile phone in the view path to add a prefix "m_" (view in a content two, such as: Index and M_index)

In fact, I just didn't think well how to add a class, and where to put ...


Switch between mobile phone and PC browser with ThinkPHP3.2.2 frame

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.