使用redis实现curd

控制器代码

 

 

<?php

namespace app\admin\controller;

use Phinx\Util\Util;
use think\Controller;
use think\Request;
use think\cache\driver\Redis as rediss;
use Redis as redist;


class Redis extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $Redis=new rediss(config());
        //获取redis中的数据
        $data=$Redis->handler()->lrange('news',0,9999);
        foreach ($data as $k=>&$v){
            $v=json_decode($v,true);
        }

        return view('index',compact('data'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
        return view('create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $Redis=new rediss(config());
        $data=$request->only('title,content');
        $data['id']='news'.'_'.time().rand(1,999);
        if ($Redis->handler()->lpush('news',json_encode($data)))
            return redirect('index');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

        //通过id获取数据到详情
        $Redis=new rediss();
        $data=$Redis->handler()->lrange('news',0,-1);
        foreach ($data as $k=>$v){
            $v=json_decode($v,true);
            if ($id==$v['id']){
               return view('read',compact('v'));
            }
        }
    }

/**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
        $Redis=new rediss();
        //获取redis中的全部数据
        $data=$Redis->handler()->lrange('news',0,9999);
        foreach ($data as $k=>&$v){
            $v=json_decode($v,true);
            if ($id==$v['id']){
                return view('edit',compact('v'));
            }
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {

        $update=$request->param();
        //链接redis
        $Redis=new rediss(config());
        //从redis中获取数据
        $data=$Redis->handler()->lrange('news',0,999);
        foreach ($data as $k=>&$v){
            $v=json_decode($v,true);
            if ($id==$v['id']){
                $data[$k]=$update;
            }
        }
        //获取到修改后的数据
        $Redis->handler()->del('news');
        foreach ($data as $v){
            $Redis->handler()->lpush('news',json_encode($v));
        }
        return redirect('index');



    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {

        //new 一个redis  应为命名空间与控制器的名字一样 所有redis应用了别名 use think\cache\driver\Redis as rediss;
        $Redis=new rediss(config());
        $data=$Redis->handler()->lrange('news',0,9999);
        foreach ($data as $k=>&$v){
            $v=json_decode($v,true);
            if ($id==$v['id']){
                unset($data[$k]);
            }
        }
        $Redis->handler()->del('news');
        foreach ($data as &$v){
            $Redis->handler()->lpush('news',json_encode($v));
        }
        return redirect('index');

    }

}

 

 

 

文章点赞操作

    public function zan(Request $request){
        //点赞的文章id
        $newid=$request->param('tid');
        //获取用户id
        $uid=session('user')['id'];
        //使用redis存入数据
        $redis = new redist();
        $redis->connect('127.0.0.1', 6379);
//        $redis->flushAll();
        //取出关于这个文章所有点赞过的用户id
        $uidArray= $redis->SMEMBERS ($newid);
        if (in_array($uid,$uidArray))  return json(['code'=>500,'msg'=>'不能重复点赞']);
        if ($redis->sAdd($newid,$uid)) return json(['code'=>200,'msg'=>'点赞成功']);
        return json(['code'=>500,'msg'=>'点赞失败']);


    }

 

 

html代码

列表展示 index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Bootstrap 实例 - 边框表格</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<table class="table table-bordered">
    <caption>列表展示</caption>
    <thead>
    <tr>
        <th>编号</th>
        <th>标题</th>
        <th>简介</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    {foreach $data as $v}
    <tr>
        <td>{$v.id}</td>
        <td>{$v.title}</td>
        <td>{$v.content}</td>
        <td>
            <a href="{:url('read')}?id={$v.id}">详情</a>
            <a href="{:url('delete')}?id={$v.id}">删除</a>
            <a href="{:url('edit')}?id={$v.id}">修改</a></td>
    </tr>
    {/foreach}
    </tbody>
</table>

</body>
</html>

 

 添加页面 create.index

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div style="width: 800px;margin-top: 50px" >

<form action="{:url('save')}" method="post">
    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input" lay-verify="title" name="title" name="title" placeholder="请输入标题"  type="text">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" name="content" placeholder="请输入内容"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">

    <button class="layui-btn" lay-filter="demo1" lay-submit="" type="submit">提交</button>
        </div>
    </div>
</form>
</div>
</body>
</html>

 

 

 

 

 获取详情页以及点赞  read.index

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文章详情</title>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

</head>
<body>

<div style="margin-top:50px" id="div">

    <div class="layui-col-md12" >
        <div class="layui-card">
            <div class="layui-card-header" style="text-align: center">{$v.title}</div>
            <div class="layui-card-body" >
               {$v.content}
            </div>
        </div>
        <div class="layui-form-item">
            <form>
                <!--        <input type="hidden" name="uid" value="">-->
                <input type="hidden" name="tid" value="{$v.id}" id="tid">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-filter="demo1" type="button"  style="margin-right: 10px" id="submit">点赞</button>
                </div>

            </form>

        </div>
    </div>

</div>
</body>
<script>
    $(function(){

        $("#submit").click(function () {
          var tid=$("#tid").val()
            $.ajax({
                type: "POST",
                url: "{:url('zan')}",
                //数据
                data: {tid:tid},
                success: function(msg){
                   if (msg.code==200){
                       alert(msg.msg)
                   }
                    if (msg.code==500){
                        alert(msg.msg)
                    }
                    // alert( "Data Saved: " + msg );
                }

            });

        })

    });

</script>
</html>

 

 

 

修改页面   edit.index

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post" action="{:url('update')}">
<!--    设置隐藏域 存入id-->
    <input type="hidden" name="id" value="{$v.id}">
    标题:<input type="text" name="title" value="{$v.title}">
    <br>
    内容:<input type="text" name="content" value="{$v.content}">
    <br>
    <input type="submit" value="修改">
</form>
</body>
</html>

 

上一篇:MongoDB实践一 (安装&CURD)


下一篇:01.06 字符串压缩