页面静态化

2 篇文章 0 订阅

1.页面静态化

A、页面纯静态化

a、构建思路

1)、获得显示数据,利用模板渲染PHP

2)、开启缓冲区

3)、将缓冲区中的内容写入文件中,从而实现动态页面静态化

b、主要使用

使用ob_start(),开启缓冲区(也可修改php配置文件来开启缓冲区),利用ob_get_contents()获取缓冲区中的内容,file_put_contents()写入文件

c、具体实现

1)、构建模板

<html>
<head>
    <meta charset="UTF-8">
    <title>新闻</title>
    <link rel="stylesheet" href="http://test.com/PHPMailer/static/css/bootstrap.min.css">
    <script src="http://test.com/PHPMailer/static/js/jquery-3.4.0.min.js"></script>
</head>
<body>
    <div class="container-fluid">
        <div class="col-md-12">
            <?php foreach ($arr as $k=>$v){ ?>
            <div class="col-md-4">
                <div class="panel panel-default">
                    <img src="<?php echo $v['image']; ?>" alt="" class="img-thumbnail" width="100%">
                    <div class="panel-heading"><a href="###"><?php echo $v['title']; ?></a></div>
                    <div class="panel-body">
                        <?php echo $v['descript']; ?>
                    </div>
                </div>
            </div>
            <?php } ?>
        </div>
        <div class="col-md-12">
            <div class="col-md-2"></div>
            <div class="col-md-8">
                <ul class="list-group" id="get_record">

                </ul>
            </div>
            <div class="col-md-2"></div>
        </div>
    </div>
</body>
<script src="http://test.com/static/static/js/ajax.js"></script>
</html>

2)、读取数据,构建静态文件

require_once './Db.php';
    $config = [
        'db' => [
            'host' => '127.0.0.1',
            'username' => 'root',
            'password' => '123456',
            'dbName' => 'static'
        ]
    ];
    try{
        $db = Db::getInstance($config['db']);
        $sql = 'SELECT * FROM news ORDER BY id DESC LIMIT 3';
        $result = mysqli_query($db->connect, $sql);
        while($row = mysqli_fetch_assoc($result)){
            $arr[] = $row;
        }
    }catch (exception $e){
        //抛出异常
        echo $e->getMessage();
    }
    //开启缓冲区(output_buffer)
    ob_start();
    //引入模板文件
    require_once('./template/model1.php');
    //获取缓冲区内容
    $s = ob_get_contents();
    //生成静态文件
    file_put_contents('./index.html', $s);

Db.php

<?php
/**
 * Created by PhpStorm.
 * User: 28195
 * Date: 2019/5/27
 * Time: 21:18
 */

class Db
{
    private $config = [];
    private static $instance;
    public $connect;
    private function __construct($config)
    {
        if(!empty($config)){
            $this->config = $config;
        }
        $this->connect();
    }

    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    public static function getInstance($config)
    {
        if(!self::$instance instanceof self){
            self::$instance = new Db($config);
        }
        return self::$instance;
    }

    private function connect()
    {
        $this->connect = mysqli_connect($this->config['host'], $this->config['username'], $this->config['password']);
        mysqli_select_db($this->connect, $this->config['dbName']);
        mysqli_query($this->connect ,"SET NAMES 'UTF8'");
    }
}

d、管理生成静态化页面的三种方式

1)、添加页面缓存时间

if(file_exists('./index.html') && time()-filemtime('./index.html')<300) {
    require_once './index.html';
}

2)、手动生成

构建后台管理界面,添加生成静态文件功能

3)、在Linux下使用crontab定时扫面程序

crontab -e //进入时刻表
//分 时 日 月 星期
crontab * * * * * programPath

B、局部静态化

a、具体思路

局部静态既是利用ajax技术来获取接口抛出数据

1)、构建局部动态所需要数据的接口

2)、利用ajax技术获取接口的数据

3)、组装数据进行显示

b、实现

1)、构建接口

简单的接口实例

require_once '../Db.php';

$config = [
    'host' => '127.0.0.1',
    'username' => 'root',
    'password' => '123456',
    'dbName' => 'static'
];
try{
    $db = Db::getInstance($config);
    $sql = 'SELECT * FROM news ORDER BY id ASC LIMIT 3';
    $result = mysqli_query($db->connect, $sql);
    while($row = mysqli_fetch_assoc($result)){
        $arr[] = $row;
    }
    echo showInfo(1, 'success', $arr);
}catch (exception $e){
    //抛出异常
    echo $e->getMessage();
}
function showInfo($code = 0, $message = 'error', $data = []) {
    $info = [
        'code' => $code,
        'message' => $message,
        'data' => $data
    ];
    return json_encode($info);
}

2)、获取并组装数据

$.ajax({
    url: 'http://test.com/static/api/getRecord.php',
    type: 'GET',
    dataType: 'json',
    data: {

    },
    success: function(res){
        html = '';
        $.each(res.data, function(k, v){
            html += '<li class="list-group-item">'+v.title+'</li>';
        });
        $("#get_record").html(html);
    },
    error: function(err){
        //TODO
        console.log(err);
    }
});

至此局部动态实现已完成

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值