Blogger Information
Blog 41
fans 0
comment 0
visits 25303
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
后台七 友情链接
自由之上
Original
966 people have browsed it

一、友情链接 数据库处理

1、创建 友情链接 数据表并填充数据

  1. #【创建】友情链接
  2. DROP TABLE IF EXISTS `friend_link`;
  3. CREATE TABLE `friend_link` (
  4. `id` int(10) NOT NULL AUTO_INCREMENT,
  5. `ord` int(3) NOT NULL COMMENT '排序',
  6. `title` varchar(50) NOT NULL COMMENT '链接名称',
  7. `url` varchar(255) NOT NULL COMMENT '链接url',
  8. `status` tinyint(1) NOT NULL COMMENT '链接状态 1:正常 0:下线',
  9. `add_time` int(10) NOT NULL COMMENT '添加时间',
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='友情链接';
  12. #【添加】友情链接
  13. INSERT INTO `friend_link` VALUES ('1', '0', '百度', 'www.baidu.com','1', '1585626833');
  14. INSERT INTO `friend_link` VALUES ('2', '0', 'php中文网', 'www.php.cn','1','1585626887');

1、打开数据库管理工具phpMyAdmin


2、执行sql语句创建表并填充数据

3、查看执行结果

二、友情链接列表 模块

1、创建 友情链接列表 视图模板

2、编辑 友情链接列表 视图模板


resources/views/admins/setting/friend_link/index.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>友链列表</title>
  5. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  6. <script type="text/javascript" src="/static/layui/layui.js"></script>
  7. </head>
  8. <body style="padding: 10px;">
  9. <div style="text-align: right;">
  10. <button class="layui-btn layui-btn-sm" onclick="add()"><i class="layui-icon layui-icon-addition"></i>添加</button>
  11. </div>
  12. <table class="layui-table">
  13. <thead>
  14. <tr>
  15. <th>ID</th>
  16. <th>友链名称</th>
  17. <th>友链URL</th>
  18. <th>排序</th>
  19. <th>状态</th>
  20. <th>添加时间</th>
  21. <th>操作</th>
  22. </tr>
  23. </thead>
  24. <tbody>
  25. @foreach($lists as $item)
  26. <tr>
  27. <td>{{$item['id']}}</td>
  28. <td>{{$item['title']}}</td>
  29. <td>{{$item['url']}}</td>
  30. <td>{{$item['ord']}}</td>
  31. <td>{{$item['status']==1?'正常':'下线'}}</td>
  32. <td>{{date('Y-m-d H:i:s',$item['add_time'])}}</td>
  33. <td>
  34. <button class="layui-btn layui-btn-xs" onclick="edit({{$item['id']}})">修改</button>
  35. <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del({{$item['id']}})">删除</button>
  36. </td>
  37. </tr>
  38. @endforeach
  39. </tbody>
  40. </table>
  41. </body>
  42. </html>
  43. <script type="text/javascript">
  44. layui.use(['layer'],function(){
  45. $ = layui.jquery;
  46. layer = layui.layer;
  47. });
  48. // 添加友链
  49. function add(){
  50. layer.open({
  51. type: 2,
  52. title: '添加友链',
  53. shade: 0.3,
  54. area: ['450px', '450px'],
  55. content: '/admins/setting/add_link'
  56. });
  57. }
  58. // 修改友链
  59. function edit(id){
  60. layer.open({
  61. type: 2,
  62. title: '修改友链',
  63. shade: 0.3,
  64. area: ['450px', '450px'],
  65. content: '/admins/setting/edit_link?id='+id
  66. });
  67. }
  68. // 删除
  69. function del(id){
  70. layer.confirm('确定要删除吗?', {
  71. icon:3,
  72. btn: ['确定','取消']
  73. }, function(){
  74. $.get('/admins/setting/del_link',{id:id},function(res){
  75. if(res.code>0){
  76. return layer.alert(res.msg,{icon:2});
  77. }
  78. layer.msg(res.msg);
  79. setTimeout(function(){
  80. window.location.reload();
  81. },1000);
  82. },'json');
  83. });
  84. }
  85. </script>

3、创建 友情链接 控制器

4、编辑 友情链接 控制器

laravel/app/Http/Controllers/admins/Setting.php

  1. <?php
  2. namespace App\Http\Controllers\admins;
  3. use App\Http\Controllers\Controller;
  4. /**
  5. * 网站设置相关
  6. */
  7. class Setting extends Controller {
  8. }

" class="reference-link">

5、友情链接 控制器处理

app/Http/Controllers/admins/Setting.php

  1. use Illuminate\Support\Facades\DB;
  2. // 友链列表
  3. public function friend_link() {
  4. $data['lists'] = DB::table('friend_link')->orderBy('id', 'desc')->lists();
  5. return view('admins/setting/friend_link/index', $data);
  6. }

6、友情链接 注册路由

  1. //显示设置-友情链接列表
  2. Route::get('/admins/setting/friend_link', [Setting::class, 'friend_link']);

7、查看效果

三、添加与保存友情链接 模块

1、创建 添加与保存友情链接 视图模板

2、编辑 添加与保存友情链接 视图模板


resources/views/admins/setting/friend_link/add.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>修改角色</title>
  5. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  6. <script type="text/javascript" src="/static/layui/layui.js"></script>
  7. </head>
  8. <body style="padding: 10px;">
  9. <div class="layui-form">
  10. @csrf
  11. <div class="layui-form-item">
  12. <label class="layui-form-label">友链名称</label>
  13. <div class="layui-input-block">
  14. <input type="text" class="layui-input" name="title">
  15. </div>
  16. </div>
  17. <div class="layui-form-item">
  18. <label class="layui-form-label">友链URL</label>
  19. <div class="layui-input-block">
  20. <input type="text" class="layui-input" name="url">
  21. </div>
  22. </div>
  23. <div class="layui-form-item">
  24. <label class="layui-form-label">排序</label>
  25. <div class="layui-input-block">
  26. <input type="number" class="layui-input" name="ord">
  27. </div>
  28. </div>
  29. <div class="layui-form-item">
  30. <label class="layui-form-label">状态</label>
  31. <div class="layui-input-block">
  32. <input type="checkbox" lay-skin="primary" title="上线" name="status">
  33. </div>
  34. </div>
  35. <div class="layui-input-block">
  36. <button class="layui-btn" onclick="save()">保存</button>
  37. </div>
  38. </div>
  39. </body>
  40. </html>
  41. <script type="text/javascript">
  42. layui.use(['form','layer'],function(){
  43. $ = layui.jquery;
  44. form = layui.form;
  45. layer = layui.layer;
  46. });
  47. // 保存
  48. function save(){
  49. var title = $('input[name="title"]').val();
  50. var url = $('input[name="url"]').val();
  51. var ord = $('input[name="ord"]').val();
  52. var status = $('input[name="status"]').is(':checked')?1:0;
  53. var _token = $('input[name="_token"]').val();
  54. $.post('/admins/setting/save_link',{title:title,url:url,ord:ord,status:status,_token:_token},function(res){
  55. if(res.code>0){
  56. return layer.alert(res.msg,{icon:2});
  57. }
  58. layer.msg(res.msg);
  59. setTimeout(function(){
  60. parent.window.location.reload();
  61. },1000);
  62. },'json');
  63. }
  64. </script>

3、添加与保存友情链接 控制器处理

  1. use Illuminate\Http\Request;
  2. // 添加友链
  3. public function add_link() {
  4. return view('admins/setting/friend_link/add');
  5. }
  6. // 保存友链
  7. public function save_link(Request $req) {
  8. $data['title'] = trim($req->title);
  9. $data['url'] = trim($req->url);
  10. $data['ord'] = (int)$req->ord;
  11. $data['status'] = (int)$req->status;
  12. $data['add_time'] = time();
  13. if ($data['title'] == '') {
  14. return json_encode(array('code' => 1, 'msg' => '友链名称不能为空'));
  15. }
  16. DB::table('friend_link')->insert($data);
  17. return json_encode(array('code' => 0, 'msg' => '保存成功'));
  18. }

4、添加与保存友情链接 注册路由

routes/web.php

  1. //添加链接 弹窗
  2. Route::get('/admins/setting/add_link', [Setting::class, 'add_link']);
  3. //保存链接 处理
  4. Route::post('/admins/setting/save_link', [Setting::class, 'save_link']);

5、查看效果

四、编辑与保存编辑友情链接 模块

1、创建 编辑与保存编辑友情链接 视图模板

2、编辑 编辑与保存编辑友情链接 视图模板


resources/views/admins/setting/friend_link/edit.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>修改角色</title>
  5. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  6. <script type="text/javascript" src="/static/layui/layui.js"></script>
  7. </head>
  8. <body style="padding: 10px;">
  9. <div class="layui-form">
  10. @csrf
  11. <input type="hidden" name="id" value="{{$item['id']}}">
  12. <div class="layui-form-item">
  13. <label class="layui-form-label">友链名称</label>
  14. <div class="layui-input-block">
  15. <input type="text" class="layui-input" name="title" value="{{$item['title']}}">
  16. </div>
  17. </div>
  18. <div class="layui-form-item">
  19. <label class="layui-form-label">友链URL</label>
  20. <div class="layui-input-block">
  21. <input type="text" class="layui-input" name="url" value="{{$item['url']}}">
  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="number" class="layui-input" name="ord" value="{{$item['ord']}}">
  28. </div>
  29. </div>
  30. <div class="layui-form-item">
  31. <label class="layui-form-label">状态</label>
  32. <div class="layui-input-block">
  33. <input type="checkbox" lay-skin="primary" title="上线" name="status" {{$item['status']==1?'checked':''}}>
  34. </div>
  35. </div>
  36. <div class="layui-input-block">
  37. <button class="layui-btn" onclick="save()">保存</button>
  38. </div>
  39. </div>
  40. </body>
  41. </html>
  42. <script type="text/javascript">
  43. layui.use(['form','layer'],function(){
  44. $ = layui.jquery;
  45. form = layui.form;
  46. layer = layui.layer;
  47. });
  48. // 保存
  49. function save(){
  50. var id = $('input[name="id"]').val();
  51. var title = $('input[name="title"]').val();
  52. var url = $('input[name="url"]').val();
  53. var ord = $('input[name="ord"]').val();
  54. var status = $('input[name="status"]').is(':checked')?1:0;
  55. var _token = $('input[name="_token"]').val();
  56. $.post('/admins/setting/edit_link_save',{id:id,title:title,url:url,ord:ord,status:status,_token:_token},function(res){
  57. if(res.code>0){
  58. return layer.alert(res.msg,{icon:2});
  59. }
  60. layer.msg(res.msg);
  61. setTimeout(function(){
  62. parent.window.location.reload();
  63. },1000);
  64. },'json');
  65. }
  66. </script>

3、编辑与保存编辑友情链接 控制器处理

app/Http/Controllers/admins/Setting.php

  1. // 修改友链
  2. public function edit_link(Request $req) {
  3. $id = (int)$req->id;
  4. $data['item'] = DB::table('friend_link')->where('id', $id)->item();
  5. return view('admins/setting/friend_link/edit', $data);
  6. }
  7. // 保存修改
  8. public function edit_link_save(Request $req) {
  9. $id = (int)$req->id;
  10. $data['title'] = trim($req->title);
  11. $data['url'] = trim($req->url);
  12. $data['ord'] = (int)$req->ord;
  13. $data['status'] = (int)$req->status;
  14. $data['add_time'] = time();
  15. if ($data['title'] == '') {
  16. return json_encode(array('code' => 1, 'msg' => '友链名称不能为空'));
  17. }
  18. DB::table('friend_link')->where('id', $id)->update($data);
  19. return json_encode(array('code' => 0, 'msg' => '保存成功'));
  20. }

4、编辑与保存编辑友情链接 注册路由

routes/web.php

  1. //编辑链接 弹窗
  2. Route::get('/admins/setting/edit_link', [Setting::class, 'edit_link']);
  3. //保存编辑链接 处理
  4. Route::post('admins/setting/edit_link_save', [Setting::class, 'edit_link_save']);

5、查看效果

五、删除友情链接 模块

1、删除友情链接 控制器处理

app/Http/Controllers/admins/Setting.php

  1. // 删除友链
  2. public function del_link(Request $req) {
  3. $id = (int)$req->id;
  4. DB::table('friend_link')->where('id', $id)->delete();
  5. return json_encode(array('code' => 0, 'msg' => '删除成功'));
  6. }

2、删除友情链接 注册路由

routes/web.php

  1. //删除友情链接 处理
  2. Route::get('/admins/setting/del_link', [Setting::class, 'del_link']);

3、查看效果

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