Rumah  >  Artikel  >  Tutorial CMS  >  phpcms实现移动端和电脑端不同模板

phpcms实现移动端和电脑端不同模板

angryTom
angryTomasal
2020-02-11 16:02:272972semak imbas

phpcms实现移动端和电脑端不同模板

phpcms实现移动端和电脑端不同模板

1、首先打开phpcms/libs/functions/global.func.php,在文件最后面加一个isMobile()方法,用来判断是否是手机端打开

 function isMobile() {
  // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
  if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
    return true;
  }
  // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
  if (isset($_SERVER['HTTP_VIA'])) {
    // 找不到为flase,否则为true
    return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
  }
  // 脑残法,判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信
  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','MicroMessenger');
    // 从HTTP_USER_AGENT中查找手机浏览器的关键字
    if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
      return true;
    }
  }
  // 协议法,因为有可能不准确,放到最后判断
  if (isset ($_SERVER['HTTP_ACCEPT'])) {
    // 如果只支持wml并且不支持html那一定是移动设备
    // 如果支持wml和html但是wml在html之前则是移动设备
    if ((strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) !== false) && (strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;text/html&#39;) === false || (strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) < strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;text/html&#39;)))) {
      return true;
    }
  }
  return false;
}

2、然后打开phpcms/modules/content/index.php,有三个地方要改的

a)找到首页的init方法,在最后加载模板的时候,做一个判断,如果是手机端打开就加载手机端模板,如果是电脑端打开就加载电脑端模板

大概在31行找到:

include template(&#39;content&#39;,&#39;index&#39;,$default_style);

改成:

if(isMobile()){
      include template('mobile','index',$default_style);
}else{
     include template(&#39;content&#39;,&#39;index&#39;,$default_style);
}

b)找到内容页的show方法,同样在最后加载模板的时候做一个判断

大概在203行找到:

include template(&#39;content&#39;,$template);

改成:

if(isMobile()){
     include template('mobile',$template);
}else{
     include template(&#39;content&#39;,$template);
}

c)找到列表页的lists方法,同样在最后加载模板的时候做一个判断

大概在265行和278行,这里有两处,找到:

include template(&#39;content&#39;,$template);
改成:
if(isMobile()){
     include template('mobile',$template);}else{     
     include template(&#39;content&#39;,$template);
}

在你当前的模板目录下新建一个mobile目录,用来存放手机端模板

如果你当前的模板目录是phpcms/templates/default,那么你就在phpcms/templates/default下面建一个mobile目录。

如果你当前的模板目录是phpcms/templates/moban,那么你就在phpcms/templates/moban下面建一个mobile目录。

这样就可以实现电脑端和手机端分别加载不同的模板。

PHP中文网,大量的免费PHPCMS教程,欢迎在线学习!

Atas ialah kandungan terperinci phpcms实现移动端和电脑端不同模板. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:phpcms数据库在哪个文件夹Artikel seterusnya:phpcms导航链接怎么修改