Blogger Information
Blog 62
fans 7
comment 2
visits 58202
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel通用后台-新闻文章系统开发
我是郭富城
Original
1359 people have browsed it

1.创建路由

  1. //新闻系统
  2. Route::get('/admins/article/index','Article@index');//文章列表
  3. Route::get('/admins/article/add','Article@add');//文章添加
  4. Route::post('/admins/article/save','Article@save');//文章保存
  5. Route::post('/admins/article/del','Article@del');//删除文章
  6. Route::get('/admins/article/cates','Article@cates');//文章分类
  7. Route::get('/admins/article/add_cate','Article@add_cate');//添加文章分类
  8. Route::post('/admins/article/save_cate','Article@save_cate');//保存文章分类
  9. Route::post('/admins/article/del_cate','Article@del_cate');//删除文章分类
  10. // 上传系统
  11. Route::post('/admins/files/upload_img','Files@upload_img');//文章缩略图上传

2. 创建后台控制器

利用laravel的artisan命令轻松创建

  1. php artisan make:controller admins/Article
  2. php artisan make:controller admins/Files

3. 控制器代码

  1. <?php
  2. namespace App\Http\Controllers\admins;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Auth;
  7. class Article extends Controller
  8. {
  9. // 添加新闻
  10. public function add(Request $request)
  11. {
  12. $aid=(int)$request->aid;
  13. $data['article']=DB::table('article')->where('id',$aid)->item();
  14. $data['contents']=DB::table('article_detail')->where('aid',$aid)->item();
  15. $data['cates']=DB::table('article_cate')->lists();
  16. return view('admins/article/add',$data);
  17. }
  18. // 保存新闻
  19. public function save(Request $request)
  20. {
  21. $aid=(int)$request->aid;
  22. $admin=Auth::user();
  23. $data['cid']=(int)$request->cid;
  24. $data['title']=trim($request->title);
  25. $data['subtitle']=trim($request->subtitle);
  26. $data['thumb']=trim($request->preview_img);
  27. $data['keywords']=trim($request->keywords);
  28. $data['descs']=trim($request->descs);
  29. $data['auther_id']=$admin->id;
  30. // echo '<pre>';
  31. // print_r($data);
  32. // print_r($admin);
  33. // return;
  34. $data['pv']=0;
  35. $data['status']=(int)$request->status;
  36. $contents=trim($request->contents);
  37. if ($data['title']==='') {
  38. exit(json_encode(['code'=>1,'message'=>'文章标题怎么可以没有呢?']));
  39. }
  40. if ($aid===0) {
  41. $data['add_time']=time();
  42. $aid = DB::table('article')->insertGetId($data);
  43. if($aid>0){
  44. DB::table('article_detail')->insert(['aid'=>$aid,'contents'=>$contents]);
  45. }
  46. } else {
  47. DB::table('article')->where('id',$aid)->update($data);
  48. DB::table('article_detail')->where('aid',$aid)->update(['aid'=>$aid,'contents'=>$contents]);
  49. }
  50. exit(json_encode(['code'=>0,'message'=>'保存新闻成功']));
  51. }
  52. // 删除新闻
  53. public function del(Request $request) {
  54. $aid=(int)$request->aid;
  55. // echo $aid;
  56. // exit;
  57. DB::table('article')->where('id',$aid)->delete();
  58. DB::table('article_detail')->where('aid',$aid)->delete();
  59. exit(json_encode(['code'=>0,'message'=>'删除文章成功']));
  60. }
  61. // 新闻列表
  62. public function index(Request $request) {
  63. $data['cates']=DB::table('article_cate')->cates('cid');
  64. // echo '<pre>';
  65. // print_r($data);
  66. // return;
  67. $data['auth']=DB::table('new_admin')->cates('id');
  68. // echo '<pre>';
  69. // print_r($data);
  70. // return;
  71. // $data['lists']=DB::table('article')->lists();
  72. $data['page']=(int)$request->page;
  73. $data['pageSize']=5;
  74. $pages= DB::table('article')->orderBy('id','desc')->pages($data['pageSize']);
  75. // 列表
  76. $data['article_lists']=$pages['lists'];
  77. // 总数
  78. $data['total']=$pages['total'];
  79. // echo '<pre>';
  80. // print_r($data);
  81. // return;
  82. return view('admins/article/index',$data);
  83. }
  84. //新闻分类
  85. public function cates() {
  86. $data['lists']=DB::table('article_cate')->lists();
  87. return view('admins/article/cates',$data);
  88. }
  89. // 添加分类
  90. public function add_cate(Request $request) {
  91. $cid=(int)$request->cid;
  92. $data['title']=DB::table('article_cate')->where('cid',$cid)->item();
  93. if(!$data['title']) {
  94. $data['title']['cid']=0;
  95. $data['title']['title']='';
  96. }
  97. // echo '<pre>';
  98. // print_r($data);
  99. return view('admins/article/add_cate',$data);
  100. }
  101. // 保存分类
  102. public function save_cate(Request $request) {
  103. $cid=(int)$request->cid;
  104. $title=trim($request->title);
  105. if ($title==='') {
  106. exit(json_encode(['code'=>1,'message'=>'分类名称不能为空']));
  107. }
  108. if ($cid===0) {
  109. DB::table('article_cate')->insert(['title'=>$title]);
  110. } else {
  111. DB::table('article_cate')->where('cid',$cid)->update(['title'=>$title]);
  112. }
  113. exit(json_encode(['code'=>0,'message'=>'保存分类成功']));
  114. }
  115. // 删除分类
  116. public function del_cate(Request $request) {
  117. $cid=(int)$request->cid;
  118. // echo $aid;
  119. // exit;
  120. DB::table('article_cate')->where('cid',$cid)->delete();
  121. exit(json_encode(['code'=>0,'message'=>'删除分类成功']));
  122. }
  123. }

4. 新闻列表页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>新闻列表</title>
  7. <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
  8. <script src="/static/plugins/layui/layui.js"></script>
  9. <link rel="stylesheet" href="/static/css/admin_index.css">
  10. </head>
  11. <body style="padding: 20px">
  12. <div class="article_add">
  13. <button class="layui-btn" onclick="add()">添加新闻</button>
  14. </div>
  15. <table class="layui-table">
  16. @csrf
  17. <thead>
  18. <tr>
  19. <th>ID</th>
  20. <th>新闻分类</th>
  21. <th>缩略图</th>
  22. <th>文章标题</th>
  23. <th>文章作者</th>
  24. <th>浏览量</th>
  25. <th>发布时间</th>
  26. <th>状态</th>
  27. <th>操作</th>
  28. </tr>
  29. </thead>
  30. <tbody>
  31. @foreach ($article_lists as $item)
  32. <tr>
  33. <td>{{$item['id']}}</td>
  34. <td>{{$cates[$item['cid']]['title']}}</td>
  35. <td><img src="{{$item['thumb']}}" /></td>
  36. <td>{{$item['title']}}</td>
  37. <td>{{$auth[$item['auther_id']]['name']}}</td>
  38. <td>{{$item['pv']}}</td>
  39. <td>{{date('Y-m-d H:i:s',$item['add_time'])}}</td>
  40. <td>{{$item['status']==0?'草稿':'已发布'}}</td>
  41. <td>
  42. <button class="layui-btn layui-btn-xs" onclick="add({{$item['id']}})">编辑</button>
  43. <button class="layui-btn layui-btn-danger layui-btn-xs" onclick="del({{$item['id']}})">删除</button>
  44. </td>
  45. </tr>
  46. @endforeach
  47. </tbody>
  48. </table>
  49. <div id="pages"></div>
  50. </body>
  51. </html>
  52. <script>
  53. layui.use(['layer', 'laypage'], function() {
  54. var layer = layui.layer;
  55. var laypage = layui.laypage;
  56. $ = layui.jquery;
  57. //执行一个laypage实例
  58. laypage.render({
  59. limit: {{$pageSize}},
  60. elem: 'pages', //注意,这里的 test1 是 ID,不用加 # 号
  61. count: {{$total}}, //数据总数,从服务端得到
  62. curr:{{$page}},
  63. layout:['count','prev', 'page', 'next','skip'],
  64. jump: function(obj, first){
  65. //obj包含了当前分页的所有参数,比如:
  66. //console.log(obj.curr); //得到当前页,以便向服务端请求对应页的数据。
  67. //console.log(obj.limit); 得到每页显示的条数
  68. //首次不执行
  69. if(!first){
  70. //do something
  71. window.location.href='?page='+obj.curr;
  72. }
  73. }
  74. });
  75. });
  76. // 修改文章
  77. function add(aid) {
  78. //iframe层
  79. layer.open({
  80. type: 2,
  81. title: aid>0?'修改新闻文章':'添加新闻文章',
  82. shadeClose: true,
  83. shade: 0.8,
  84. area: ['80%', '90%'],
  85. content: '/admins/article/add?aid='+aid, //iframe的url
  86. btn: ['保存'],
  87. yes: function(index, layero) {
  88. var body = layer.getChildFrame('body', index);
  89. var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象,执行iframe页的方法:
  90. iframeWin.save();
  91. }
  92. });
  93. }
  94. //删除文章
  95. function del(aid) {
  96. //询问框
  97. layer.confirm('是否确定删除?', {
  98. icon: 3,
  99. btn: ['确定', '取消'] //按钮
  100. }, function() {
  101. var _token = $('input[name="_token"]').val();
  102. $.post('/admins/article/del', {
  103. aid: aid,
  104. _token: _token
  105. }, function(res) {
  106. if (res.code > 0) {
  107. return layer.alert(res.message, {
  108. icon: 2
  109. });
  110. }
  111. layer.msg('删除文章成功');
  112. setTimeout(function() {
  113. window.location.reload();
  114. }, 1000)
  115. }, 'json');
  116. });
  117. }
  118. </script>

5 新增文章页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>添加文章</title>
  6. <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
  7. <script src="/static/plugins/layui/layui.js"></script>
  8. <link rel="stylesheet" href="/static/css/admin_index.css">
  9. <!-- 配置文件 -->
  10. <script type="text/javascript" src="/static/plugins/ueditor/ueditor.config.js"></script>
  11. <!-- 编辑器源码文件 -->
  12. <script type="text/javascript" src="/static/plugins/ueditor/ueditor.all.js"></script>
  13. </head>
  14. <body style="padding: 20px">
  15. <div class="layui-form">
  16. @csrf
  17. <input type="hidden" name="aid" value="{{$article['id']}}">
  18. <div class="layui-form-item">
  19. <label class="layui-form-label">文章标题</label>
  20. <div class="layui-input-block">
  21. <input type="text" name="title" class="layui-input" value="{{$article['title']}}">
  22. </div>
  23. </div>
  24. <div class="layui-form-item">
  25. <label class="layui-form-label">文章副标题</label>
  26. <div class="layui-input-block">
  27. <input type="text" name="subtitle" class="layui-input" value="{{$article['subtitle']}}">
  28. </div>
  29. </div>
  30. <div class="layui-form-item">
  31. <label class="layui-form-label">文章分类</label>
  32. <div class="layui-input-block">
  33. <select name="cid">
  34. <option value="0">请选择新闻的分类</option>
  35. @foreach ($cates as $cate)
  36. <option value="{{$cate['cid']}}" {{$cate['cid']===$article['cid']?'selected':''}}>{{$cate['title']}}</option>
  37. @endforeach
  38. </select>
  39. </div>
  40. </div>
  41. <div class="layui-form-item">
  42. <label class="layui-form-label">封面图</label>
  43. <div class="layui-input-block">
  44. <img id="preview_img" src="{{$article['thumb']}}" style="height: 30px">
  45. <button type="button" class="layui-btn" id="img_upload">
  46. <i class="layui-icon">&#xe67c;</i>上传图片
  47. </button>
  48. </div>
  49. </div>
  50. <div class="layui-form-item">
  51. <label class="layui-form-label">关键词</label>
  52. <div class="layui-input-block">
  53. <input type="text" name="keywords" class="layui-input" value="{{$article['keywords']}}">
  54. </div>
  55. </div>
  56. <div class="layui-form-item">
  57. <label class="layui-form-label">描述</label>
  58. <div class="layui-input-block">
  59. <textarea name="descs" placeholder="请输入内容" class="layui-textarea">{{$article['descs']}}</textarea>
  60. </div>
  61. </div>
  62. <!-- 文章详情 -->
  63. <div class="layui-form-item">
  64. <label class="layui-form-label">文章内容</label>
  65. <div class="layui-input-block">
  66. <!-- 加载编辑器的容器 -->
  67. <script id="container" name="contents" type="text/plain">{!!$contents['contents']!!}</script>
  68. </div>
  69. </div>
  70. <div class="layui-form-item">
  71. <label class="layui-form-label">状态</label>
  72. <div class="layui-input-block">
  73. <input type="checkbox" name="status" {{$article['status']===1?'checked':''}} lay-skin="primary" title="发布">
  74. </div>
  75. </div>
  76. </div>
  77. </body>
  78. </html>
  79. <script>
  80. layui.use(['upload','form'], function(){
  81. var upload = layui.upload;
  82. var form = layui.form;
  83. $ = layui.jquery;
  84. var _token=$('input[name="_token"]').val();
  85. //执行实例
  86. var uploadInst = upload.render({
  87. elem: '#img_upload' //绑定元素
  88. ,url: '/admins/files/upload_img' //上传接口
  89. ,data: {_token:_token}
  90. ,done: function(res){
  91. //上传完毕回调
  92. // console.log('success');
  93. $('#preview_img').attr('src',res.data.src);
  94. }
  95. ,error: function(){
  96. //请求异常回调
  97. }
  98. });
  99. // 实例化编辑器
  100. // var ue = UE.getContent();
  101. // 不要有var
  102. ue = UE.getEditor('container', {
  103. autoHeightEnabled: true
  104. ,initialFrameWidth: '100%' //初始化编辑器宽度,默认1000
  105. ,minFrameWidth: '760' //编辑器拖动时最小宽度,默认800
  106. ,initialFrameHeight: 450 //初始化高度
  107. ,minFrameHeight:630
  108. ,initialContent:'请输入详细的内容'
  109. });
  110. });
  111. // 文章保存
  112. function save() {
  113. // var title = $.trim($('input[name="title"]').val());
  114. // var subtitle = $.trim($('input[name="subtitle"]').val());
  115. // var cid = parseInt($('select[name="cid"]').val());
  116. // var preview_img = $('#preview_img').arrt('src');
  117. // var keywords = $('input[name="keywords"]').val();
  118. // var descs = $('textarea[name="descs"]').val();
  119. // var content = UE.getContent();
  120. // var status = $('input[name="status"]').is(':checked')?1:0;
  121. var data={};
  122. data.aid=$('input[name="aid"]').val();
  123. data.title=$.trim($('input[name="title"]').val());
  124. data.subtitle=$.trim($('input[name="subtitle"]').val());
  125. data.cid=parseInt($('select[name="cid"]').val());
  126. data.preview_img=$('#preview_img').attr('src');
  127. data.keywords=$('input[name="keywords"]').val();
  128. data.descs=$('textarea[name="descs"]').val();
  129. // 全局变量
  130. data.contents=ue.getContent();
  131. data.status=$('input[name="status"]').is(':checked')?1:0;
  132. data._token=$('input[name="_token"]').val();
  133. if (data.title==='') {
  134. return layer.alert('填写文章标题',{icon:2});
  135. }
  136. if (isNaN(data.cid) || data.cid<=0) {
  137. return layer.alert('请勾选文章分类',{icon:2});
  138. }
  139. $.post('/admins/article/save',data,function(res){
  140. if (res.code > 0) {
  141. return layer.alert(res.message, {
  142. icon: 2
  143. });
  144. }
  145. layer.msg('添加文章成功');
  146. setTimeout(function() {
  147. parent.window.location.reload();
  148. }, 1000)
  149. },'json');
  150. }
  151. </script>

6 增加文章分类页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>文章分类添加</title>
  7. <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
  8. <script src="/static/plugins/layui/layui.js"></script>
  9. <link rel="stylesheet" href="/static/css/admin_index.css">
  10. </head>
  11. <body style="padding:20px;">
  12. </body>
  13. <div class="layui-form">
  14. @csrf
  15. <input type="hidden" name="cid" value="{{$title['cid']}}">
  16. <div class="layui-form-item">
  17. <label class="layui-form-label">分类名称</label>
  18. <div class="layui-input-block">
  19. <input type="text" class="layui-input" name="title" value="{{$title['title']}}">
  20. </div>
  21. </div>
  22. <!-- <div class="layui-input-block">
  23. <button class="layui-btn" onclick="save()">保存</button>
  24. </div> -->
  25. </div>
  26. </html>
  27. <script>
  28. layui.use(['layer'], function() {
  29. var layer = layui.layer;
  30. $ = layui.jquery;
  31. });
  32. function save() {
  33. var title = $.trim($('input[name="title"]').val());
  34. var cid = $('input[name="cid"]').val();
  35. if (title === '') {
  36. return layer.alert('请填写分类标题', {
  37. icon: 2
  38. });
  39. }
  40. var _token = $('input[name="_token"]').val();
  41. $.post('/admins/article/save_cate', {
  42. cid: cid,
  43. title: title,
  44. _token: _token
  45. }, function(res) {
  46. if (res.code > 0) {
  47. return layer.alert(res.message, {
  48. icon: 2
  49. });
  50. }
  51. layer.msg('添加分类成功');
  52. setTimeout(function() {
  53. parent.window.location.reload();
  54. }, 1000)
  55. }, 'json');
  56. }
  57. </script>

7 文章分类列表页

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>新闻分类列表</title>
  7. <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
  8. <script src="/static/plugins/layui/layui.js"></script>
  9. <link rel="stylesheet" href="/static/css/admin_index.css">
  10. </head>
  11. <body style="padding: 20px;">
  12. <div style="float: right;padding: 10px;">
  13. <button class="layui-btn" onclick="add()">添加</button>
  14. </div>
  15. <table class="layui-table">
  16. @csrf
  17. <thead>
  18. <tr>
  19. <th>ID</th>
  20. <th>分类名称</th>
  21. <th>操作</th>
  22. </tr>
  23. </thead>
  24. <tbody>
  25. @foreach ($lists as $item)
  26. <tr>
  27. <td>{{$item['cid']}}</td>
  28. <td>{{$item['title']}}</td>
  29. <td>
  30. <button class="layui-btn layui-btn-xs" onclick="add({{$item['cid']}})">修改</button>
  31. <button class="layui-btn layui-btn-danger layui-btn-xs" onclick="del({{$item['cid']}})">删除</button>
  32. </td>
  33. </tr>
  34. @endforeach
  35. </tbody>
  36. </table>
  37. </body>
  38. </html>
  39. <script>
  40. layui.use(['layer'], function() {
  41. var layer = layui.layer;
  42. $ = layui.jquery;
  43. });
  44. function add(cid) {
  45. //iframe层
  46. layer.open({
  47. type: 2,
  48. title: cid > 0 ? '修改分类' : '添加分类',
  49. shadeClose: true,
  50. shade: 0.8,
  51. area: ['500px', '400px'],
  52. content: '/admins/article/add_cate?cid=' + cid, //iframe的url
  53. btn: ['保存'],
  54. yes: function(index, layero) {
  55. var body = layer.getChildFrame('body', index);
  56. var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象,执行iframe页的方法:
  57. iframeWin.save();
  58. }
  59. });
  60. }
  61. function del(cid) {
  62. //询问框
  63. layer.confirm('是否确定删除分类?', {
  64. icon: 3,
  65. btn: ['确定', '取消'] //按钮
  66. }, function() {
  67. var _token = $('input[name="_token"]').val();
  68. $.post('/admins/article/del_cate', {
  69. cid: cid,
  70. _token: _token
  71. }, function(res) {
  72. if (res.code > 0) {
  73. return layer.alert(res.message, {
  74. icon: 2
  75. });
  76. }
  77. layer.msg('删除分类成功');
  78. setTimeout(function() {
  79. window.location.reload();
  80. }, 1000)
  81. }, 'json');
  82. });
  83. }
  84. </script>

8 演示地址

http://www.php520.vip/
测试账号:admin 密码:123456 权限:超管
测试账号:ceshi 密码:123456 权限:普管

9 总结

一个完整的博客应用不能没有后台管理系统。laravel框架结合layui快速开发一个小型门户博客文章系统,不仅速度快,而且维护方便。

Correcting teacher:WJWJ

Correction status:qualified

Teacher's comments:写的非常不错!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post