Blogger Information
Blog 62
fans 7
comment 2
visits 58230
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel通用后台-视频管理系统开发
我是郭富城
Original
1568 people have browsed it

1. 创建路由

  1. // 视频中心
  2. Route::get('/admins/video/index','Video@index');//视频列表
  3. Route::get('/admins/video/add','Video@add');//视频添加
  4. Route::post('/admins/video/save','Video@save');//视频保存
  5. Route::post('/admins/video/del','Video@del');//视频保存
  6. // 仿抖音
  7. Route::get('/douyin/index','Douyin@index');//抖音首页

2. 创建后台和前端控制器

利用laravel的artisan命令轻松创建

  1. php artisan make:controller admins/Video
  2. php artisan make:controller Douyin

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. // 视频管理
  8. class Video extends Controller
  9. {
  10. //视频列表
  11. public function index(Request $request) {
  12. $data['cates']=DB::table('video_cate')->cates('vcid');
  13. $data['auth']=DB::table('new_admin')->cates('id');
  14. $data['area']=DB::table('areas')->where('pid',0)->cates('code');
  15. $data['page']=(int)$request->page;
  16. $data['pageSize']=5;
  17. $pages= DB::table('video')->orderBy('id','desc')->pages($data['pageSize']);
  18. // 列表
  19. $data['video_list']=$pages['lists'];
  20. // 总数
  21. $data['total']=$pages['total'];
  22. // echo '<pre>';
  23. // print_r($data);
  24. // return;
  25. return view('admins/video/index',$data);
  26. }
  27. //视频增加
  28. public function add(Request $request){
  29. $vid=(int)$request->vid;
  30. $data['video']=DB::table('video')->where('id',$vid)->item();
  31. $data['cates']=DB::table('video_cate')->lists();
  32. $data['areas']=DB::table('areas')->where('pid',0)->lists();
  33. // echo '<pre>';
  34. // print_r($data);
  35. // return;
  36. return view('admins/video/add',$data);
  37. }
  38. // 视频保存
  39. public function save(Request $request)
  40. {
  41. $vid=(int)$request->vid;//视频的id
  42. $admin=Auth::user();
  43. $data['vcid']=(int)$request->vcid;
  44. $data['title']=trim($request->title);
  45. $data['area']=(int)$request->area;
  46. $data['thumb']=trim($request->preview_img);
  47. $data['video_url']=trim($request->video_url);
  48. $data['auther_id']=$admin->id;
  49. $data['pv']=0;
  50. $data['status']=(int)$request->status;
  51. // echo '<pre>';
  52. // echo $vid;
  53. // print_r($data);
  54. // print_r($admin);
  55. // return;
  56. if ($data['title']==='') {
  57. exit(json_encode(['code'=>1,'message'=>'视频标题怎么可以没有呢?']));
  58. }
  59. if ($vid===0) {
  60. $data['add_time']=time();
  61. DB::table('video')->insert($data);
  62. } else {
  63. DB::table('video')->where('id',$vid)->update($data);
  64. }
  65. exit(json_encode(['code'=>0,'message'=>'保存视频成功']));
  66. }
  67. // 删除视频
  68. public function del(Request $request) {
  69. $vid=(int)$request->vid;
  70. // echo $aid;
  71. // exit;
  72. DB::table('video')->where('id',$vid)->delete();
  73. exit(json_encode(['code'=>0,'message'=>'删除视频成功']));
  74. }
  75. }

4. 前端控制器代码

  1. <?php
  2. namespace App\Http\Controllers;
  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 Douyin extends Controller
  8. {
  9. //仿抖音首页
  10. public function index() {
  11. $data['video_list']=DB::table('video')->where('status',1)->limit(6)->orderBy('id','desc')->lists();
  12. // echo '<pre>';
  13. // print_r($data);
  14. // return;
  15. $data['area']=DB::table('areas')->where('pid',0)->cates('code');
  16. return view('douyin/index',$data);
  17. }
  18. }

5 后台视频列表页

  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. <th>操作</th>
  29. </tr>
  30. </thead>
  31. <tbody>
  32. @foreach ($video_list as $item)
  33. <tr>
  34. <td>{{$item['id']}}</td>
  35. <td>{{$cates[$item['vcid']]['title']}}</td>
  36. <td><img src="{{$item['thumb']}}" /></td>
  37. <td>{{$item['title']}}</td>
  38. <td>{{$auth[$item['auther_id']]['name']}}</td>
  39. <td>{{$area[$item['area']]['name']}}</td>
  40. <td>{{$item['pv']}}</td>
  41. <td>{{date('Y-m-d H:i:s',$item['add_time'])}}</td>
  42. <td>{{$item['status']==0?'未上架':'已上架'}}</td>
  43. <td>
  44. <button class="layui-btn layui-btn-xs" onclick="add({{$item['id']}})">编辑</button>
  45. <button class="layui-btn layui-btn-danger layui-btn-xs" onclick="del({{$item['id']}})">删除</button>
  46. </td>
  47. </tr>
  48. @endforeach
  49. </tbody>
  50. </table>
  51. <div id="pages"></div>
  52. </body>
  53. </html>
  54. <script>
  55. layui.use(['layer', 'laypage'], function() {
  56. var layer = layui.layer;
  57. var laypage = layui.laypage;
  58. $ = layui.jquery;
  59. //执行一个laypage实例
  60. laypage.render({
  61. limit: {{$pageSize}},
  62. elem: 'pages', //注意,这里的 test1 是 ID,不用加 # 号
  63. count: {{$total}}, //数据总数,从服务端得到
  64. curr:{{$page}},
  65. layout:['count','prev', 'page', 'next','skip'],
  66. jump: function(obj, first){
  67. //obj包含了当前分页的所有参数,比如:
  68. //console.log(obj.curr); //得到当前页,以便向服务端请求对应页的数据。
  69. //console.log(obj.limit); 得到每页显示的条数
  70. //首次不执行
  71. if(!first){
  72. //do something
  73. window.location.href='?page='+obj.curr;
  74. }
  75. }
  76. });
  77. });
  78. // 添加、修改视频
  79. function add(vid) {
  80. //iframe层
  81. layer.open({
  82. type: 2,
  83. title: vid>0?'修改视频':'添加视频',
  84. shadeClose: true,
  85. shade: 0.8,
  86. area: ['80%', '60%'],
  87. content: '/admins/video/add?vid='+vid, //iframe的url
  88. btn: ['保存'],
  89. yes: function(index, layero) {
  90. var body = layer.getChildFrame('body', index);
  91. var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象,执行iframe页的方法:
  92. iframeWin.save();
  93. }
  94. });
  95. }
  96. //删除文章
  97. function del(vid) {
  98. //询问框
  99. layer.confirm('是否确定删除?', {
  100. icon: 3,
  101. btn: ['确定', '取消'] //按钮
  102. }, function() {
  103. var _token = $('input[name="_token"]').val();
  104. $.post('/admins/video/del', {
  105. vid: vid,
  106. _token: _token
  107. }, function(res) {
  108. if (res.code > 0) {
  109. return layer.alert(res.message, {
  110. icon: 2
  111. });
  112. }
  113. layer.msg('删除视频成功');
  114. setTimeout(function() {
  115. window.location.reload();
  116. }, 1000)
  117. }, 'json');
  118. });
  119. }
  120. </script>

6 新增发布视频代码

  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. </head>
  10. <body style="padding: 20px">
  11. <div class="layui-form">
  12. @csrf
  13. <input type="hidden" name="vid" value="{{$video['id']}}">
  14. <div class="layui-form-item">
  15. <label class="layui-form-label">视频标题</label>
  16. <div class="layui-input-block">
  17. <input type="text" name="title" class="layui-input" value="{{$video['title']}}">
  18. </div>
  19. </div>
  20. <div class="layui-form-item">
  21. <label class="layui-form-label">视频分类</label>
  22. <div class="layui-input-block">
  23. <select name="vcid">
  24. <option value="0">请选择视频的分类</option>
  25. @foreach ($cates as $cate)
  26. <option value="{{$cate['vcid']}}" {{$cate['vcid']===$video['vcid']?'selected':''}}>{{$cate['title']}}</option>
  27. @endforeach
  28. </select>
  29. </div>
  30. </div>
  31. <div class="layui-form-item">
  32. <label class="layui-form-label">地区</label>
  33. <div class="layui-input-block">
  34. <select name="area">
  35. <option value="0">请选择地区</option>
  36. @foreach ($areas as $area)
  37. <option value="{{$area['code']}}" {{$area['code']===$video['area']?'selected':''}}>{{$area['name']}}</option>
  38. @endforeach
  39. </select>
  40. </div>
  41. </div>
  42. <div class="layui-form-item">
  43. <label class="layui-form-label">封面图</label>
  44. <div class="layui-input-block">
  45. <img id="preview_img" src="{{$video['thumb']}}" style="height: 30px">
  46. <button type="button" class="layui-btn" id="img_upload">
  47. <i class="layui-icon"></i>上传图片
  48. </button>
  49. </div>
  50. </div>
  51. <div class="layui-form-item">
  52. <label class="layui-form-label">视频地址</label>
  53. <div class="layui-input-block">
  54. <input type="text" name="video_url" class="layui-input" value="{{$video['video_url']}}">
  55. </div>
  56. </div>
  57. <div class="layui-form-item">
  58. <label class="layui-form-label">状态</label>
  59. <div class="layui-input-block">
  60. <input type="checkbox" name="status" {{$video['status']===1?'checked':''}} lay-skin="primary" title="发布">
  61. </div>
  62. </div>
  63. </div>
  64. </body>
  65. </html>
  66. <script>
  67. layui.use(['upload','form'], function(){
  68. var upload = layui.upload;
  69. var form = layui.form;
  70. $ = layui.jquery;
  71. var _token=$('input[name="_token"]').val();
  72. //执行实例
  73. var uploadInst = upload.render({
  74. elem: '#img_upload' //绑定元素
  75. ,url: '/admins/files/upload_img' //上传接口
  76. ,data: {_token:_token}
  77. ,done: function(res){
  78. //上传完毕回调
  79. // console.log('success');
  80. $('#preview_img').attr('src',res.data.src);
  81. }
  82. ,error: function(){
  83. //请求异常回调
  84. }
  85. });
  86. });
  87. // 视频保存
  88. function save() {
  89. var data={};
  90. data.vid=$('input[name="vid"]').val();//编辑的视频id
  91. data.title=$.trim($('input[name="title"]').val());
  92. data.vcid=parseInt($('select[name="vcid"]').val());//视频分类id
  93. data.preview_img=$('#preview_img').attr('src');
  94. data.video_url=$('input[name="video_url"]').val();
  95. data.status=$('input[name="status"]').is(':checked')?1:0;
  96. data._token=$('input[name="_token"]').val();
  97. data.area=parseInt($('select[name="area"]').val());
  98. if (data.title==='') {
  99. return layer.alert('填写视频标题',{icon:2});
  100. }
  101. if (isNaN(data.vcid) || data.vcid<=0) {
  102. return layer.alert('请勾选视频分类',{icon:2});
  103. }
  104. $.post('/admins/video/save',data,function(res){
  105. if (res.code > 0) {
  106. return layer.alert(res.message, {
  107. icon: 2
  108. });
  109. }
  110. layer.msg('添加视频成功');
  111. setTimeout(function() {
  112. parent.window.location.reload();
  113. }, 1000)
  114. },'json');
  115. }
  116. </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, maximum-scale=1, user-scalable=no">
  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/douyin.css">
  10. </head>
  11. <body>
  12. <!-- 头部导航 -->
  13. <div class="header_title">
  14. <span class="layui-icon layui-icon-left"></span>
  15. <span>播放广场</span>
  16. <span class="layui-icon layui-icon-set-fill"></span>
  17. </div>
  18. <!-- 幻灯片 -->
  19. <div class="layui-carousel" id="carousel">
  20. <div carousel-item>
  21. <div><img src="/static/images/douyin/banner.jpg" alt="幻灯片1"></div>
  22. <div><img src="/static/images/douyin/banner.jpg" alt="幻灯片2"></div>
  23. <div><img src="/static/images/douyin/banner.jpg" alt="幻灯片3"></div>
  24. <div><img src="/static/images/douyin/banner.jpg" alt="幻灯片4"></div>
  25. <div><img src="/static/images/douyin/banner.jpg" alt="幻灯片5"></div>
  26. </div>
  27. </div>
  28. <!-- 视频列表 -->
  29. <div class="video_list">
  30. @foreach ($video_list as $video)
  31. <div class="video_item">
  32. <img src="{{$video['thumb']}}" alt="">
  33. <div class="descs">
  34. <div class="area">{{$area[$video['area']]['name']}}</div>
  35. <div class="title">{{$video['title']}}</div>
  36. </div>
  37. </div>
  38. @endforeach
  39. </div>
  40. </body>
  41. </html>
  42. <script>
  43. layui.use(['carousel'], function() {
  44. var carousel = layui.carousel;
  45. //建造实例
  46. carousel.render({
  47. elem: '#carousel',
  48. width: '100%', //设置容器宽度
  49. height: '20vh', //设置容器高度
  50. arrow: 'none' //始终显示箭头
  51. //,anim: 'updown' //切换动画方式
  52. });
  53. });
  54. </script>

8 演示地址

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

9 总结

Laravel的语法优雅,很多方法都不用看文档直接就能推算出来;而且是全栈,该有的都有了,方便快速构建原型;扩展齐全,大量的扩展包,有 WordPress 的感觉;

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