ob缓存生成静态页

 写一个class类在类中写一个ob缓存的方法,下面查询出来的数据$data(是你自己要查的数据,不是必须$this->sel()这样写)。$data这个变量在content.php做的是循环变量。           使用时实例化类调用这个ob方法就可以使用了

 

如果你懂php相信你会看懂的。。。

在tp5中的写法

<?php    
class cache{
    public function ob(){    
        //开启缓存
        ob_start();
        //查询的数据
        $data= $this->sel();
        //调用的模板位置
        $file = "content.php";
        include ($file);
        //清除
        $content = ob_get_clean();
        //生成的静态页位置 type是文件夹的名字
        $last = "./type/$id.html";
        file_put_contents($last,$content);
    }
}

content.php的代码(要调用的模板的)

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<table border="1">
    <tr>
        <td>标题</td>
        <td>内容</td>
    </tr>
    <tr>
        <td><?php echo $data['title']?></td>
        <td><?php echo $data['content']?></td>
    </tr>
</table>
</body>
</html>

在laravel框架中的写法

在控制器中写的方法

//生成静态页
    public function jt($id){
        ob_start();
        $arr =$res=Zc::article_sel($id);
        //模板位置
        $file_dir=base_path()."/resources/views/zc/muban.blade.php";
        include ($file_dir);
        if(!file_exists("storage/html")){
            mkdir("storage/html");
        }
        //静态页生成的位置
        $html_dir="storage/html/$id.html";
        file_put_contents($html_dir,ob_get_clean());
        //跳转到静态页
        echo "<script>location.href='http://www.lr.com/storage/html/$id.html'</script>";
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值