效果圖 首先是資料庫 /* Navicat MySQL Data Transfer Source Server : xm Source Server Version : 50553 Source Host : localhost:3306 Source Database : test Target ...
效果圖
首先是資料庫
/* Navicat MySQL Data Transfer Source Server : xm Source Server Version : 50553 Source Host : localhost:3306 Source Database : test Target Server Type : MYSQL Target Server Version : 50553 File Encoding : 65001 Date: 2019-01-17 15:00:23 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for test_topic -- ---------------------------- DROP TABLE IF EXISTS `test_topic`; CREATE TABLE `test_topic` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `content` text NOT NULL, `user_id` int(11) NOT NULL, `created_at` int(11) NOT NULL, `is_delete` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=57 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of test_topic -- ---------------------------- INSERT INTO `test_topic` VALUES ('48', 'php是最好的語言4', '沙發是的範德薩撒範德薩發敖德薩多第三方撒地方大SAV大V', '33', '1547610289', '0'); INSERT INTO `test_topic` VALUES ('49', 'ceshi ', '大大沙發斯蒂芬', '33', '1547610343', '0'); INSERT INTO `test_topic` VALUES ('50', '的是非得失', '第三個教廷', '33', '1547610400', '0'); INSERT INTO `test_topic` VALUES ('51', '多福多壽', '阿道夫', '33', '1547610507', '0'); INSERT INTO `test_topic` VALUES ('52', '王企鵝去吧', '無法讓我', '33', '1547633627', '0'); INSERT INTO `test_topic` VALUES ('53', '我的錢多額', 'werewolf未確認翁', '33', '1547633684', '0'); INSERT INTO `test_topic` VALUES ('54', '2323232', '232323', '33', '1547633838', '0'); INSERT INTO `test_topic` VALUES ('55', '2323232', '232323', '33', '1547634620', '0'); INSERT INTO `test_topic` VALUES ('56', '56ttt', '232323跳跳糖', '33', '1547634633', '0'); -- ---------------------------- -- Table structure for test_user -- ---------------------------- DROP TABLE IF EXISTS `test_user`; CREATE TABLE `test_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL COMMENT '用戶名', `email` varchar(100) CHARACTER SET latin1 NOT NULL COMMENT '郵箱', `avatar` varchar(255) CHARACTER SET latin1 NOT NULL, `password` varchar(100) CHARACTER SET latin1 NOT NULL COMMENT '密碼', `created_at` int(11) NOT NULL, `is_delete` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=35 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of test_user -- ---------------------------- INSERT INTO `test_user` VALUES ('23', 'zxj', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547607239', '0'); INSERT INTO `test_user` VALUES ('29', '', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547608741', '0'); INSERT INTO `test_user` VALUES ('30', '', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547608915', '0'); INSERT INTO `test_user` VALUES ('31', '', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547609026', '0'); INSERT INTO `test_user` VALUES ('32', '', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547609076', '0'); INSERT INTO `test_user` VALUES ('33', 'root', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547609167', '0'); INSERT INTO `test_user` VALUES ('34', 'root', '[email protected]', 'images/avatar.jpg', '9db06bcff9248837f86d1a6bcf41c9e7', '1547609206', '0');
模型層
application/common/model/Base.php
<?php namespace app\common\model; use think\Model; class Base extends Model{ protected $autoWriteTimestamp = true; protected $createTime='created_at'; /** * 新增邏輯 * @auth cyy * @param array $data [description] * @return int */ public function add($data = []) { if(empty($data) || !is_array($data)) { return false; } $this->allowField(true)->save($data); return $this->id; } }
application/common/model/Topic.php
<?php namespace app\common\model; class Topic extends Base { //查詢屬於哪個用戶 public function user() { // 連接topic表中的user_id return $this->hasOne('User','id','user_id'); } //獲取所有留言 public static function getTopics() { $topics = model("Topic") ->order("id", "desc") ->select(); return $topics; } //獲取某條留言 public static function getTopic($id) { $topic = self::find(['id' => $id]); //print_r(model("Topic")->getLastSql());die(); return $topic; } }
application/common/model/User.php
<?php namespace app\common\model; use think\Model; class User extends Base{ //查詢賬號 public function isUser($condition) { return $this->where($condition) ->limit(1) ->select(); } }
控制器層
application/index/controller/Base.php
<?php namespace app\index\controller; use think\Controller; class Base extends Controller { /** * 空操作 * @auth singwa * @param [type] $name [description] * @return [type] [description] */ public function _empty($name) { return $name; } }
application/index/controller/Index.php
<?php namespace app\index\controller; use think\Db; use app\common\model\User; use app\common\model\Topic as TopicModel; class Index extends Base { public function index() { //獲取session $name=session('name'); //獲取留言列表 $topic=model('Topic'); $topics=$topic->getTopics(); //輸出到模板 echo $this->fetch('',[ 'name'=>$name, 'topics'=>$topics, ]); } //註冊 public function register() { echo $this->fetch(); } //添加賬號 public function add(){ //如果有數據提交 if(!empty(input('param.'))){ //接收數據 $userdata=input('param.'); $name=$userdata['username']; $email=$userdata['email']; $password=$userdata['password']; $password_confirmation=$userdata['password_confirmation']; //驗證確認密碼是否正確 if($password_confirmation!=$password){ $this->error('兩次密碼輸入不一致','index/register'); } $data=[ 'name'=>$name, 'email'=>$email, 'password'=>$password, ]; //存儲數據 $user=model('User'); $id=$user->add($data); $this->success('註冊成功','index/login'); } } //登錄 public function login() { //檢測是否登錄 if(session('name')){ return $this->error('您已經登錄',url('index/index')); } echo $this->fetch(); //獲取session $name=session('name'); //輸出到頭部模板 echo $this->fetch('common/header',[ 'name'=>$name, ]); } //登錄檢測 public function isUser(){ //如果有數據提交 if(!empty(input('param.'))){ //接收數據 $userdata=input('param.'); $login=$userdata['login']; $password=$userdata['password']; //查詢條件 $condition1=[ 'name'=>$login, 'password'=>$password, ]; $condition2=[ 'email'=>$login, 'password'=>$password, ]; //查詢 $user=model('User'); $login1=$user->isUser($condition1); $login2=$user->isUser($condition2); if(!empty($login1)){ //姓名登錄 session('id',$login1[0]->id); session('name',$login1[0]->name); $this->success('登錄成功','index/index'); }else if(!empty($login2)){ //郵箱登錄 session('id',$login2[0]->id); session('name',$login2[0]->name); $this->success('登錄成功','index/index'); }else{ $this->error('登錄失敗','index/login'); } } } //退出 public function logout(){ session('name',null); $this->redirect('index/index'); } }
application/index/controller/Topic.php
<?php namespace app\index\controller; use think\Db; use app\common\model\Topic as TopicModel; class Topic extends Base { public function _initialize(){ //檢測是否登錄 if(!session('name')){ $this->error('您沒有登錄',url('index/login')); } } //發帖頁面 public function new_topic(){ echo $this->fetch(); //獲取session $name=session('name'); //輸出到頭部模板 echo $this->fetch('common/header',[ 'name'=>$name, ]); } //添加新帖 public function add_topic(){ //如果有數據提交 if(!empty(input('param.'))){ //接收數據 $topicdata=input('param.'); $title=$topicdata['title']; $content=$topicdata['content']; $user_id=session('id'); $data=[ 'title'=>$title, 'content'=>$content, 'user_id'=>$user_id, ]; //存儲數據 $topic=model('Topic'); $id=$topic->add($data); $this->success('添加成功','index/index'); } } //編輯新帖頁面 public function detail_topic(){ //接收id $id=input('param.id'); //查詢這條帖子的發佈者 $condition=[ 'id'=>$id, ]; $user=model('Topic')->where($condition)->find(); $user_id=$user->user_id; //判斷是否有許可權編輯帖子 if(session('id') != $user_id){ $this->error('您沒有許可權修改!'); } $topic=model('Topic')->getTopic($id); echo $this->fetch('',[ 'topic'=>$topic, ]); //獲取session $name=session('name'); //輸出到頭部模板 echo $this->fetch('common/header',[ 'name'=>$name, ]); } //更新帖子數據 public function update_topic(){ //如果有數據提交 if(!empty(input('param.'))){ //接收數據 $topicdata=input('param.'); $title=$topicdata['title']; $content=$topicdata['content']; $id=$topicdata['id']; $data=[ 'title'=>$title, 'content'=>$content, ]; $condition=[ 'id'=>$id, ]; //存儲數據 $res=model('Topic')->where($condition)->update($data); if($res){ $this->success('編輯成功','index/index'); }else{ $this->error('編輯失敗'); } } } }
視圖層
application/index/view/common/header.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>test論壇</title> <link rel="stylesheet" href="<?=STATIC_URL?>css/all-df86af5803.css"> <script src="<?=STATIC_URL?>js/all-17476e6cc3.js"></script> </head> <body class="forum" data-page="forum"> <div class="header"> <nav class="navbar navbar-inverse navbar-fixed-top navbar-default"> <div class="container"> <div class="navbar-header" id="navbar-header"> <a href="<?=url('index/index');?>" class="navbar-brand"><img src="<?=STATIC_URL;?>images/logo.png"></a> </div> <div id="main-nav-menu"> <ul class="nav navbar-nav"> <li class="active"> <a href="<?=url('index/index');?>"> <i class="fa fa-home"></i> <span class="hidden-xs hidden-sm">首頁</span> </a> </li> </ul> </div> <ul class="nav navbar-nav navbar-right"> <?php if(!isset($name)): ?> <li><a href="<?=url('index/register');?>" id="signup-btn">註冊</a></li> <li><a href="<?=url('index/login');?>" id="login-btn">登錄</a></li> <?php else: ?> <li><a href="<?=url('topic/new_topic');?>">發帖</a></li> <li> <ul class="nav user-bar navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><?php echo $name; ?> <span class="caret"></span></a> <button class="navbar-toggle" type="button" data-toggle="dropdown" role="button" aria-expanded="false"> <span class="sr-only">Toggle</span> <i class="fa fa-reorder"></i> </button> <ul class="dropdown-menu" role="menu"> <li class='divider'></li> <li> <a href="<?=url('index/logout');?>" onclick="return confirm('你確定要退出嗎?')"><i class="fa fa-sign-out"></i>退出</a> </li> </ul> </li> </ul> </li> <?php endif; ?> </ul> </div> </nav> </div>
application/index/view/common/footer.html
<footer class="footer"> <div class="container"> <div class="copyright"> <blockquote class="pull-left"> <p>test論壇</p> <p>Copyright ©