一个很简单的功能点,解决方案也有很多种,比如使用原生PHP通过分析UA来判断。
但如果你的后端用的是ThinkPHP5框架的话,解决这个功能点就更简单了,因为TP5的源码中已经封装好了,我们可以直接使用。
//手机端首页的controller层代码 public function index() { if(request()->isMobile()){//如果是手机端访问重定向到手机端路径 $this->redirect('wap/index/index'); } else { include template('index'); } }
//PC端首页的controller层代码 public function index() { if(!request()->isMobile()){//如果是PC端访问重定向到PC端路径 $this->redirect('index/index/index'); } else { include template('index'); } }
上面的举例代码就是使用了TP5封装好的方法,这个方法的源码在request文件中,方法名称isMobile(),原理同样是通过分析用户的UA来判断访问来源。