ThinkPHP5.0.7支持手机电脑双模板的修改
来源:互联网 发布:centos怎么用root登录 编辑:程序博客网 时间:2024/05/01 21:48
1.创建一个view类继承系统的view类,建议位置(\application\common\controller\View.php)代码如下
<?phpnamespace app\common\controller;use think\view as DefauleView;use think\Hook;class View extends DefauleView{ /** * 解析和获取模板内容 用于输出 * @param string $template 模板文件名或者内容 * @param array $vars 模板输出变量 * @param array $replace 替换内容 * @param array $config 模板参数 * @param bool $renderContent 是否渲染内容 * @return string * @throws Exception * * 修改使之能加载手机模板 * */ public function fetch($template = '', $vars = [], $replace = [], $config = [], $renderContent = false) { $request = \think\Request::instance(); if (!$template && $request->isMobile()){ $view = 'view-mobile'; $template_mobile = strtolower(APP_PATH.request()->module().DS.$view.DS.$request->controller().DS.$request->action().'.html'); if (is_file($template_mobile)){ $template = $template_mobile; } } // 模板变量 $vars = array_merge(self::$var, $this->data, $vars); // 页面缓存 ob_start(); ob_implicit_flush(0); // 渲染输出 $method = $renderContent ? 'display' : 'fetch'; $this->engine->$method($template, $vars, $config); // 获取并清空缓存 $content = ob_get_clean(); // 内容过滤标签 Hook::listen('view_filter', $content); // 允许用户自定义模板的字符串替换 $replace = array_merge($this->replace, $replace); if (!empty($replace)) { $content = strtr($content, $replace); } return $content; }}
2.修改系统的Template.php文件,位置\thinkphp\library\think\Template.php
1)修改fetch方法 line186
public function fetch($template, $vars = [], $config = []) { if ($vars) { $this->data = $vars; } if ($config) { $this->config($config); } if (!empty($this->config['cache_id']) && $this->config['display_cache']) { // 读取渲染缓存 $cacheContent = Cache::get($this->config['cache_id']); if (false !== $cacheContent) { echo $cacheContent; return; } } $template = $this->parseTemplateFile($template); if ($template) {/*修改$cacheFile,系统默认是一个页面一个缓存,支持手机后,需要分电脑和手机单独缓存页面*/ $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($template.Request::instance()->isMobile()) . '.' . ltrim($this->config['cache_suffix'], '.'); if (!$this->checkCache($cacheFile)) { // 缓存无效 重新模板编译 $content = file_get_contents($template); $this->compiler($content, $cacheFile); } // 页面缓存 ob_start(); ob_implicit_flush(0); // 读取编译存储 $this->storage->read($cacheFile, $this->data); // 获取并清空缓存 $content = ob_get_clean(); if (!empty($this->config['cache_id']) && $this->config['display_cache']) { // 缓存页面输出 Cache::set($this->config['cache_id'], $content, $this->config['cache_time']); } echo $content; } }
2)修改parseTemplateFile方法 line1077 1080
private function parseTemplateFile($template) { if ('' == pathinfo($template, PATHINFO_EXTENSION)) { if (strpos($template, '@')) { list($module, $template) = explode('@', $template); } if (0 !== strpos($template, '/')) { $template = str_replace(['/', ':'], $this->config['view_depr'], $template); } else { $template = str_replace(['/', ':'], $this->config['view_depr'], substr($template, 1)); } if ($this->config['view_base']) { $module = isset($module) ? $module : Request::instance()->module(); $path = $this->config['view_base'] . ($module ? $module . DS : ''); } else { $path = isset($module) ? APP_PATH . $module . DS . basename($this->config['view_path']) . DS : $this->config['view_path']; } $template_tmp = $template;//添加的 $template = $path . $template . '.' . ltrim($this->config['view_suffix'], '.'); } //对于手机模板处理 添加的 if (Request::instance()->isMobile()){ $template_mobile = str_replace('view','view-mobile',$path) . $template_tmp . '.' . ltrim($this->config['view_suffix'], '.'); if (is_file($template_mobile)){ $template = $template_mobile; } } if (is_file($template)) { // 记录模板文件的更新时间 $this->includeFile[$template] = filemtime($template); return $template; } else { throw new TemplateNotFoundException('template not exists:' . $template, $template); } }
3.使用view类
$view = new \app\common\controller\View();$this->view = $view;return $this->view->fetch();前台html文件中,文件包含用{include file="public/header" /}
0 0
- ThinkPHP5.0.7支持手机电脑双模板的修改
- 支持UniJa和JTWT业务的双模手机LG W800
- phpcms v9电脑pc站+手机wap移动端双模板共用数据库数据同步可同步生成静态文件
- 双模手机介绍:什么是双模手机?
- Thinkphp5.0完全相同的域名下访问电脑和手机网站
- phpcms实现手机端与PC端双模板的方法与详细教程
- phpcmsv9实现手机端与PC端双模板的方法与详细教程(转)
- WM Platform双模手机的开发流程及框架
- WM Platform双模手机的开发流程及框架
- 关于GSM/CDMA双模手机
- 【整理】1、C网和G网双模手机是否可同时支持C网和G网上网?2、上网的同时是否可以打电话?3、呼叫等待和呼叫保持的概念与“双通”手机
- 【整理】双模双待、双卡双待、双模双待双通、双频手机
- 【整理】双模双待、双卡双待、双模双待双通、双频手机
- android如何修改手机连接电脑显示在电脑上的名字(MTK)
- andorid程序同时支持手机 平板电脑
- Gemini工程如何修改双模设置默认为双模开
- springmvc双模板引擎
- 在android 2.2 上开发GC双模手机的考虑---RIL电话管理的设计
- Java反射机制在实际开发中的用法
- 我想告诉自己几句话
- CAN收发器TJA1040简介
- ReentrantLock可重入锁的使用场景
- 数据结构之数组和链表的区别和List的三个子类的特点
- ThinkPHP5.0.7支持手机电脑双模板的修改
- 设计模式的六大原则
- 强连通分量的Garbow算法
- 蜀绣歌词将我们带入唯美的意境
- CardView 介绍
- caffe 源码解析系列
- Unity3D学习(9)——DOTween简单仿写
- WePY 在手机充值小程序中的应用与实践
- HDU 1873 看病要排队 优先队列