解决思路就是控制缓存规则,如果发现是手机登录,切换缓存路径。
入口文件 index.php
//切换模板
if(isMobile()){
$_SESSION['theme_name'] = 'mobile';
$_SESSION['theme_path'] = 'm/';
}else{
unset($_SESSION['theme_name']);
unset($_SESSION['theme_path']);
}
//判断是否为移动端登录
function isMobile(){
if(isset($_SERVER['HTTP_X_WAP_PROFILE'])) return true;
if(isset($_SERVER['HTTP_VIA']))return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
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');if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))return true;}
if(isset($_SERVER['HTTP_ACCEPT'])){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;
}
home配置文件:'HTML_CACHE_ON' => true,
'HTML_CACHE_RULES' => array(
'Index:index' => array('../{$_SESSION.theme_path}{:action}',0),
'Index:msgbox' => array('{$_SESSION.theme_path}{:action}',0),
'Index:login' => array('{$_SESSION.theme_path}{:action}',0),
),
公用控制器(如果你没有可自行决定存放位置):class CommonController extends Controller{
public function construct(){
if($_SESSION['theme_name']){$this->theme($_SESSION['theme_name']);} //主题切换
}
此接口写的很活。支持无限模板切换。如果你有多个模板,修改入口文件的session定义即可。或改用其他方式。
最佳答案
