Based on the Thinkphp framework, the corresponding pages of the PC station and the mobile station are realized as the same link

Based on the thinkphp framework, the corresponding pages of the PC station and the mobile station are realized as the same link, and the page is not an adaptive type.

Usually the mobile station and the pc station need to have corresponding controllers. In order to complete the website quickly and efficiently, we use the same controller for the mobile station and the pc station.

First of all , the page naming convention needs to name the html files corresponding to the mobile station and the pc station with the same name. For example, the about us page, the mobile phone is named about.html, and the about us of the pc station should be named about.html.

Secondly , we need to place the page files (folders) corresponding to the pc and mobile stations in the view folder under the front-end module (usually index or home, according to our own directory structure). For example, here we have the pc folder and the m folder, which correspond to the static page folders of the pc station and the mobile station respectively.
Insert picture description here
Again , reconfigure the template path in the configuration file under the front-end module (mine is the index module);

return[
    'template'=>[
        'view_path'    => VIEW_PATH,
    ]
];

Finally , in the base class or common.php file, you need to determine whether the phone or the computer is, and then jump to different modules. I configured it in the common.php file, (path; application/common.php)

//判断手机还是pc,可以换成自己的方法或者其他判断方法
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['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;
}

//根据不同设备,跳转不同的路径
if(!isMobile()){
    
    
    //$this->viewtype='mobile@';
    define('VIEW_PATH','../application/index/view/pc/view');//此处换成自己的pc模块路径
}else{
    
    
    define('VIEW_PATH','../application/index/view/m/view/');//此处换成自己的手机模块路径
}

After this configuration is completed, the corresponding pages of the pc station and the mobile station can be realized as the same link.

Guess you like

Origin blog.csdn.net/qq_36129701/article/details/106665214