TP5 determine the phone and PC side

Source: Internet
Author: User
This article mainly introduces about TP5 judgment mobile phone side and PC side, has a certain reference value, now share to everyone, have the need for friends can refer to

TP5 and PC-side judgment in the mobile phone

First, the use of custom decision method

  1. The application>common.php IsMobile method used to determine the device login is first written in the public file:

    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 and is suitable for TPM development if (Isset ($_server[' http_client ') && ' phoneclient ' = = $_server[' http_client '])    return true; If VIA information contains WAP, it must be a mobile device, some service associations block the information if (Isset ($_server[' Http_via '))//cannot be 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 ', ' IP Hone ', ' ipod ', ' blackberry ', ' Meizu ', ' Android ', ' NetFront ', ' Symbian ', ' UCWeb ', ' windowsce ', ' palm ', ' Operamini ', ' Opera        Mobi ', ' Openwave ', ' nexusone ', ' cldc ', ' MIDP ', ' wap ', ' mobile '; Look for the keyword if (Preg_match ("/(") of the phone's browser from Http_user_agent. Implode (' | ', $clientkeywords). ")/I", Strtolower ($_server[' Http_user_agent '])))        {return true; }}//protocol method, because it may not be accurate, put to the Last Judgment if (Isset ($_server[' http_accept ')) {//If only WML is supported and HTML is not supported that must be mobile//IF Support for WML and HTML but WML before HTML is the mobile device if ((Strpos ($_server[' http_accept '), ' VND.WAP.WML ')!== false) && (Strpos ($_se rver[' http_accept '], ' text/html ') = = = False | | (Strpos ($_server[' http_accept '), ' vnd.wap.wml ') < Strpos ($_server[' http_accept '], ' text/html '))))        {return true; }} return false;}
  2. Then override the Fetch method in the base class controller base of the application>index>controller>base.php foreground index module:

    /** * Load template output (PC and phone) * @accessprotected * @paramstring $template template file name * @paramstring $mobiletemplate Phone template file name * @paramarray $ VARs Template Output Variable * @paramarray $replace template replacement * @paramarray $config template parameters * @returnmixed */protectedfunction fetch ($template = ', $m Obiletemplate = ", $vars = [], $replace = [], $config = []) {if (IsMobile ()) {return $this->view->fetch (    $mobiletemplate, $vars, $replace, $config);    } else {return $this->view->fetch ($template, $vars, $replace, $config); }}
  3. Finally, at the end application>index>controller>Index of the method index of the controller that inherits the base class base, the path to the PC side and the path of the mobile end are passed in.

    return $this->fetch (' Default/index/index ', ' mobile/index/index ');

    The front default/index/index is the corresponding path of the PC side, followed by the mobile/index/index mobile side of the corresponding path.

Second, the use of TP5 own Judgment method (recommended use, has been optimized)

Through the above method we can see, although the effect, but each time to pass two paths, it is cumbersome.

    1. The application>common.php constants VIEW_PATH used to determine device logons are first written in the public file:

      if (\think\request::instance ()->ismobile ()) {    define (' View_path ', __dir__. '/.. /application/index/view/mobile/');} else {    define (' View_path ', __dir__. '/.. /application/index/view/default/');}
    2. Then application>index>config.php replace the template variable in the configuration file config.php of the module index:

      return [    "template" =>[        //Templates path        ' view_path ' = View_path,    ],];
    3. Finally, in the Controller's method only need to fetch a path directly.

      return $this->fetch ();

TIP: The directory structure of the foreground page:
Note that the directory structure under default and mobile remains consistent

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.