创建后台内容管理:视图代码
`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>内容列表</title>
<meta charset="utf-8">
<link rel="stylesheet" href="/static/css/style.css" media="all">
<script type="text/javascript" src="/static/js/jquery3.4.1.js"></script>
<script type="text/javascript" src="/static/layer/layer.js"></script>
<script type="text/javascript" src="/static/js/phpcn.js"></script>
</head>
<body>
<div class="phpcn-pd-10 phpcn-bg-fff">
@csrf
<div class="phpcn-list-header phpcn-mb-20 phpcn-clear">
<div class="phpcn-row">
<div class="phpcn-title phpcn-ps-r">内容列表</div>
<button class="phpcn-button phpcn-bg-black phpcn-button-edit" onclick="add()" style="color:#fff;float:right;">添加</button>
<div class="phpcn-col-md6 phpcn-mt-20">
<div class="phpcn-form phpcn-bg-fff ">
<div class="phpcn-form-item phpcn-bg-fff ">
<div class="phpcn-input-block phpcn-col-md3">
<select name="type">
<option value="1" {{$type==1?'selected':''}}>标题</option>
<option value="2" {{$type==2?'selected':''}}>作者</option>
</select>
</div>
<div class="phpcn-input-block phpcn-col-md3">
<input type="text" name="wd" value="{{$wd}}" placeholder="搜索内容" class="phpcn-input">
</div>
<div class="phpcn-input-block phpcn-col-md2 phpcn-ml-10">
<button class="phpcn-button" onclick="searchs()">搜索</button>
</div>
</div>
</div>
</div>
</div>
</div>
<table class="phpcn-table">
<thead>
<tr>
<th>ID</th>
<!-- <th>模版</th> -->
<th>标题</th>
<th>分类</th>
<th>作者</th>
<th>修改时间</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach($lists as $item)
<tr>
<td>{{$item[‘id’]}}</td>
<td>{{$item[‘title’]}}</td>
<td>{{$item[‘cate_id’]}}</td>
<td>{{$item[‘author’]}}</td>
<td>{{date(‘Y-m-d H:i:s’,$item[‘edit_time’])}}</td>
<td>{{$item[‘status’]}}</td>
<td>
<button class="phpcn-button phpcn-bg-black phpcn-button-edit" onclick="add({{$item['id']}})">修改</button>
<button class="phpcn-button phpcn-bg-red phpcn-button-edit" onclick="del({{$item['id']}})">删除</button>
</td>
</tr>
@endforeach
</tbody>
</table>
<!-- 分页 -->
{{$links}}
</div>
</body>
</html>
<script type="text/javascript">
function searchs() {
// 获取这两个输入框的值
var type = $(‘select[name=”type”]’).val();
var wd = $.trim($(‘input[name=”wd”]’).val());
// 把这两个值往后传,刷新这个地址,再加上传参数
window.location.href = ‘?type=’ + type + ‘&wd=’ + wd;
}
//添加/修改
function add(id) {
window.location.href = '/admins/content/add';
}
//删除
function del(id) {
//询问框
layer.confirm('您确定要删除吗?', {
icon: 3,
btn: ['确定', '取消'] //按钮
}, function() {
$.post('/admins/content/del', {
id: id,
_token: $('input[name="_token"]').val()
}, function(res) {
if (res.code > 0) {
return layer.alert(res.msg, {
icon: 2
});
}
layer.msg(res.msg);
setTimeout(function() {
window.location.reload();
}, 1000); //定时器
}, 'json');
});
}
</script>`
后台内容管理:路由
Route::get(‘admins/content/index’, ‘Content@index’);
内容管理:后台控制器
`<?php
namespace xpcms\Http\Controllers\admins;
use Illuminate\Http\Request;
use xpcms\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
//内容管理
class Content extends Controller{
//内容;列表
public function index(Request $res){
//取值
$type=(int)$res->type;
$wd= trim($res->wd);
$appends=[‘type’=>$type,’wd’=>$wd];
//调0用扩展方法 查询数据库!
// $dbObj = DB::table(‘xpcms_article’);
// if ($type == 1) {
// $dbObj = $dbObj->where('title', 'like', '%' . $wd . '%');
// }
// if ($type == 2) {
// $dbObj = $dbObj->where('author', 'like', '%' . $wd . '%');
// }
// $data= $dbObj->pages(2, $appends);
// 数组 条件查询
$_where=[];
if ($type==1 && $wd) {
$_where[]= ['title', 'like', '%' . $wd . '%'];
}
if ($type == 2 && $wd) {
$_where [] = ['author', 'like', '%' . $wd . '%'];
}
$data=DB::table('xpcms_article')->where($_where)->pages(5,$appends);
//返回视图
$data['type']=$type;
$data['wd']=$wd;
return view('/admins/content/index',$data);
}
//内容添加
public function add(){
return view('/admins/content/add');
}
//内容保存
public function save(Request $req){
#数组
$data['title'] = trim($req->title);
$data['subtitle']=trim($req->subtitle);
$data['cover_img']=trim($req->cover_img);
$data['seo_title']=trim($req->seo_title);
$data['keyword']=trim($req->keyword);
$data['descs']=trim($req->descs);
$data['author']=trim($req->author);
$data['add_time']=time();
$data['edit_time']=time();
#内容数组
$detail['contents']=trim($req->contents);
if ($data['title']=='') {
exit(json_encode(array('code'=>1,'msg'=>'内容不能为空')));
}
#插入数据库
$aid = DB::table('xpcms_article')->insertGetId($data); //插入返回自增ID
$detail['aid'] = $aid;
DB::table('xpcms_article_content')->insert($detail);
exit(json_encode(array('code' => 0, 'msg' => '保存成功')));
}
//删除
public function del(Request $req)
{
$id = (int) $req->id;
DB::table('xpcms_article')->where('id', $id)->delete();
DB::table('xpcms_article_content')->where('aid', $id)->delete();
exit(json_encode(array('code' => 0, 'msg' => '删除成功!')));
}
}
`