thinkphp5项目--个人博客(一)

thinkphp5项目--个人博客(一)

项目地址

fry404006308/personalBlog: personalBlog
https://github.com/fry404006308/personalBlog

一、数据表创建

 

 

 

 1 /*
 2 Navicat MySQL Data Transfer
 3 
 4 Source Server         : localhost_3306
 5 Source Server Version : 50553
 6 Source Host           : localhost:3306
 7 Source Database       : personalblog
 8 
 9 Target Server Type    : MYSQL
10 Target Server Version : 50553
11 File Encoding         : 65001
12 
13 Date: 2018-04-09 04:27:03
14 */
15 
16 DROP database IF EXISTS `personalBlog`;
17 create database personalBlog character set utf8 collate utf8_general_ci;
18 use personalBlog;
19 
20 SET FOREIGN_KEY_CHECKS=0;
21 
22 -- ----------------------------
23 -- Table structure for tp_admin
24 -- ----------------------------
25 DROP TABLE IF EXISTS `tp_admin`;
26 CREATE TABLE `tp_admin` (
27   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
28   `username` varchar(255) DEFAULT NULL COMMENT '管理员名称',
29   `password` varchar(255) DEFAULT NULL COMMENT '管理员密码',
30   PRIMARY KEY (`id`)
31 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
32 
33 -- ----------------------------
34 -- Records of tp_admin
35 -- ----------------------------
36 
37 -- ----------------------------
38 -- Table structure for tp_article
39 -- ----------------------------
40 DROP TABLE IF EXISTS `tp_article`;
41 CREATE TABLE `tp_article` (
42   `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '文章id',
43   `title` varchar(255) DEFAULT NULL COMMENT '文章标题',
44   `author` varchar(255) DEFAULT NULL COMMENT '文章作者',
45   `desc` varchar(255) DEFAULT NULL COMMENT '文章简介',
46   `keywords` varchar(255) DEFAULT NULL COMMENT '文章的关键词',
47   `content` text COMMENT '文章内容',
48   `pic` varchar(255) DEFAULT NULL COMMENT '文章缩略图,是一个地址',
49   `click` int(10) unsigned zerofill DEFAULT NULL COMMENT '点击数',
50   `state` int(10) unsigned zerofill DEFAULT NULL COMMENT '文章状态 0:不推荐  1:推荐',
51   `time` int(11) DEFAULT NULL COMMENT '文章发布时间,时间戳',
52   `cateid` int(11) DEFAULT NULL COMMENT '文章所属的栏目',
53   PRIMARY KEY (`id`)
54 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
55 
56 -- ----------------------------
57 -- Records of tp_article
58 -- ----------------------------
59 
60 -- ----------------------------
61 -- Table structure for tp_cate
62 -- ----------------------------
63 DROP TABLE IF EXISTS `tp_cate`;
64 CREATE TABLE `tp_cate` (
65   `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '栏目id',
66   `catename` varchar(255) DEFAULT NULL COMMENT '栏目名称',
67   PRIMARY KEY (`id`)
68 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
69 
70 -- ----------------------------
71 -- Records of tp_cate
72 -- ----------------------------
73 
74 -- ----------------------------
75 -- Table structure for tp_tags
76 -- ----------------------------
77 DROP TABLE IF EXISTS `tp_tags`;
78 CREATE TABLE `tp_tags` (
79   `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '标签id',
80   `tagname` varchar(255) DEFAULT NULL COMMENT '标签名',
81   PRIMARY KEY (`id`)
82 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
83 
84 -- ----------------------------
85 -- Records of tp_tags
86 -- ----------------------------
sql创建数据库及表完整代码

二、前后台模板分离

拿后台举例

上面部分和左边的公共部分被放在了公共文件夹中

引用如下:

top部分

1     <!-- 头部 -->
2     {include file="common/top"}
3     <!-- /头部 -->

left部分

1             <!-- Page Sidebar -->
2             {include file="common/left"}
3             <!-- /Page Sidebar -->

三、管理员添加

 1     public function add()
 2     {
 3         //判断是否为post方法提交
 4         if(request()->isPost()){
 5             // dump(input('post.'));
 6             // 如果提交消息成功,我们就添加消息到数据库
 7             
 8    //          // 服务器端对数据进行验证
 9    //          $validate = new Validate([
10             //     'username' => 'require|max:25',
11             //     'password' => 'require|min:32'
12             // ]);
13             // 1、接收传递过来的数据
14 
15             $data=[
16                 'username'=>input('username'),
17                 'password'=>md5(input('password')),
18             ];
19 
20             $validate = Loader::validate('Admin');
21             if(!$validate->scene('add')->check($data)){
22                 $this->error($validate->getError()); die;
23             }
24 
25    //          if (!$validate->check($data)) {
26             //     dump($validate->getError());
27             //     die;
28             // }
29 
30             // if添加成功,就指向success页面
31             if(Db::name('admin')->insert($data)){
32                 return $this->success('添加管理员成功!!','lst');
33             }else{
34                 return $this->error('添加管理员失败!!');
35             }
36             return;
37         }
38         return view();
39     }

四、数据验证及验证场景详解

 1 use thinkValidate;
 2 
 3     public function add()
 4     {
 5         //判断是否为post方法提交
 6         if(request()->isPost()){
 7             // dump(input('post.'));
 8             // 如果提交消息成功,我们就添加消息到数据库
 9             
10    //          // 服务器端对数据进行验证
11    //          $validate = new Validate([
12             //     'username' => 'require|max:25',
13             //     'password' => 'require|min:32'
14             // ]);
15             // 1、接收传递过来的数据
16 
17             $data=[
18                 'username'=>input('username'),
19                 'password'=>md5(input('password')),
20             ];
21 
22             $validate = Loader::validate('Admin');
23             if(!$validate->scene('add')->check($data)){
24                 $this->error($validate->getError()); die;
25             }
26 
27    //          if (!$validate->check($data)) {
28             //     dump($validate->getError());
29             //     die;
30             // }
31 
32             // if添加成功,就指向success页面
33             if(Db::name('admin')->insert($data)){
34                 return $this->success('添加管理员成功!!','lst');
35             }else{
36                 return $this->error('添加管理员失败!!');
37             }
38             return;
39         }
40         return view();
41     }

五、管理员列表及分页 

1     public function lst()
2     {
3         // 分页输出列表 每页显示3条数据
4         $list = AdminModel::paginate(3);
5         $this->assign('list',$list);
6         return view('list');
7     }
 1 <body>
 2     <!-- 头部 -->
 3     {include file="common/top"}
 4     <!-- /头部 -->
 5     
 6     <div class="main-container container-fluid">
 7         <div class="page-container">
 8 
 9             <!-- Page Sidebar -->
10             {include file="common/left"}
11             <!-- /Page Sidebar -->
12 
13             <!-- Page Content -->
14             <div class="page-content">
15                 <!-- Page Breadcrumb -->
16                 <div class="page-breadcrumbs">
17                     <ul class="breadcrumb">
18                                         <li>
19                         <a href="{:url('index/index')}">系统</a>
20                     </li>
21                                         <li class="active">用户管理</li>
22                                         </ul>
23                 </div>
24                 <!-- /Page Breadcrumb -->
25 
26                 <!-- Page Body -->
27                 <div class="page-body">
28                     
29 <button type="button" tooltip="添加用户" class="btn btn-sm btn-azure btn-addon" onClick="javascript:window.location.href = '{:url('admin/add')}'"> <i class="fa fa-plus"></i> Add
30 </button>
31 <div class="row">
32     <div class="col-lg-12 col-sm-12 col-xs-12">
33         <div class="widget">
34             <div class="widget-body">
35                 <div class="flip-scroll">
36                     <table class="table table-bordered table-hover">
37                         <thead class="">
38                             <tr>
39                                 <th class="text-center" width="10%">ID</th>
40                                 <th class="text-center">用户名称</th>
41                                 <th class="text-center" width="20%">操作</th>
42                             </tr>
43                         </thead>
44                         <tbody>
45                             {volist name="list" id="value"}
46                             <tr>
47                                 <td align="center">{$value.id}</td>
48                                 <td align="center">{$value.username}</td>
49                                 <td align="center">
50                                     <a href="/admin/user/edit/id/6.html" class="btn btn-primary btn-sm shiny">
51                                         <i class="fa fa-edit"></i> 编辑
52                                     </a>
53                                     
54                                     {if condition="$value['id'] neq 1"}
55                                     <a href="#" onClick="warning('确实要删除吗', '{:url('admin/del',array('id'=>$value['id']))}')" class="btn btn-danger btn-sm shiny">
56                                         <i class="fa fa-trash-o"></i> 删除
57                                     </a>
58                                     {/if}
59 
60                                 </td>
61                             </tr>
62                             {/volist}
63                            
64                                                     </tbody>
65                     </table>
66                     <div class="text-right" style="margin-top: 10px">
67                         {$list->render()}
68                     </div>
69                     
70                 </div>
71                 <div>
72                                     </div>
73             </div>
74         </div>
75     </div>
76 </div>
77 
78                 </div>
79                 <!-- /Page Body -->
80             </div>
81             <!-- /Page Content -->
82         </div>    
83     </div>
84 
85         <!--Basic Scripts-->
86     <script src="__PUBLIC__/style/jquery_002.js"></script>
87     <script src="__PUBLIC__/style/bootstrap.js"></script>
88     <script src="__PUBLIC__/style/jquery.js"></script>
89     <!--Beyond Scripts-->
90     <script src="__PUBLIC__/style/beyond.js"></script>
91     
92 
93 
94 </body>

分页之所以代码特别少是因为配置里面是有配置好了

1     //分页配置
2     'paginate'               => [
3         'type'      => 'bootstrap',
4         'var_page'  => 'page',
5         'list_rows' => 15,
6     ],

六、模型

1 <?php
2 namespace appadminmodel;
3 
4 use thinkModel;
5 class Admin extends Model
6 {
7     
8 }

控制器中

 1 use appadminmodelAdmin as AdminModel;
 2 
 3 
 4     public function lst()
 5     {
 6         // 分页输出列表 每页显示3条数据
 7         $list = AdminModel::paginate(3);
 8         $this->assign('list',$list);
 9         return view('list');
10     }
原文地址:https://www.cnblogs.com/Renyi-Fan/p/8762418.html