ThinkPHP5.0.7支持手机电脑双模板的修改

本文介绍了一种在现有系统中增加手机模板支持的方法。通过继承系统View类并重写fetch方法来实现自动检测并加载手机专用模板。同时,还介绍了如何修改Template类以支持独立缓存手机模板。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建一个view类继承系统的view类,建议位置(\application\common\controller\View.php)代码如下

<?php
namespace 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" /}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值