博客列表 >11月23日作业 laravel框架渲染CMS后台内容管理:文章列表及分页功能 !

11月23日作业 laravel框架渲染CMS后台内容管理:文章列表及分页功能 !

星空的博客
星空的博客原创
2019年12月01日 17:06:00725浏览

创建后台内容管理:视图代码

`<!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;
}

  1. //添加/修改
  2. function add(id) {
  3. window.location.href = '/admins/content/add';
  4. }
  5. //删除
  6. function del(id) {
  7. //询问框
  8. layer.confirm('您确定要删除吗?', {
  9. icon: 3,
  10. btn: ['确定', '取消'] //按钮
  11. }, function() {
  12. $.post('/admins/content/del', {
  13. id: id,
  14. _token: $('input[name="_token"]').val()
  15. }, function(res) {
  16. if (res.code > 0) {
  17. return layer.alert(res.msg, {
  18. icon: 2
  19. });
  20. }
  21. layer.msg(res.msg);
  22. setTimeout(function() {
  23. window.location.reload();
  24. }, 1000); //定时器
  25. }, 'json');
  26. });
  27. }

</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’);

  1. // if ($type == 1) {
  2. // $dbObj = $dbObj->where('title', 'like', '%' . $wd . '%');
  3. // }
  4. // if ($type == 2) {
  5. // $dbObj = $dbObj->where('author', 'like', '%' . $wd . '%');
  6. // }
  7. // $data= $dbObj->pages(2, $appends);
  8. // 数组 条件查询
  9. $_where=[];
  10. if ($type==1 && $wd) {
  11. $_where[]= ['title', 'like', '%' . $wd . '%'];
  12. }
  13. if ($type == 2 && $wd) {
  14. $_where [] = ['author', 'like', '%' . $wd . '%'];
  15. }
  16. $data=DB::table('xpcms_article')->where($_where)->pages(5,$appends);
  17. //返回视图
  18. $data['type']=$type;
  19. $data['wd']=$wd;
  20. return view('/admins/content/index',$data);
  21. }
  22. //内容添加
  23. public function add(){
  24. return view('/admins/content/add');
  25. }
  26. //内容保存
  27. public function save(Request $req){
  28. #数组
  29. $data['title'] = trim($req->title);
  30. $data['subtitle']=trim($req->subtitle);
  31. $data['cover_img']=trim($req->cover_img);
  32. $data['seo_title']=trim($req->seo_title);
  33. $data['keyword']=trim($req->keyword);
  34. $data['descs']=trim($req->descs);
  35. $data['author']=trim($req->author);
  36. $data['add_time']=time();
  37. $data['edit_time']=time();
  38. #内容数组
  39. $detail['contents']=trim($req->contents);
  40. if ($data['title']=='') {
  41. exit(json_encode(array('code'=>1,'msg'=>'内容不能为空')));
  42. }
  43. #插入数据库
  44. $aid = DB::table('xpcms_article')->insertGetId($data); //插入返回自增ID
  45. $detail['aid'] = $aid;
  46. DB::table('xpcms_article_content')->insert($detail);
  47. exit(json_encode(array('code' => 0, 'msg' => '保存成功')));
  48. }
  49. //删除
  50. public function del(Request $req)
  51. {
  52. $id = (int) $req->id;
  53. DB::table('xpcms_article')->where('id', $id)->delete();
  54. DB::table('xpcms_article_content')->where('aid', $id)->delete();
  55. exit(json_encode(array('code' => 0, 'msg' => '删除成功!')));
  56. }

}

`

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议