效果图
首先是数据库
/* 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) ,`content` text user_id` tinyint(1) NULL DEFAULT '0'PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT57 DEFAULT CHARSET=utf8; Records of test_topicINSERT INTO `test_topic` VALUES (48',PHP是最好的语言4沙发是的范德萨撒范德萨发敖德萨多第三方撒地方大SAV大V331547610289); 49ceshi 大大沙发斯蒂芬154761034350的是非得失第三个梵蒂冈154761040051多福多寿阿道夫154761050752王企鹅去吧无法让我154763362753我的钱多额werewolf未确认翁154763368454232323223232315476338385515476346205656ttt232323跳跳糖1547634633); Table structure for test_user `test_user`; `test_user` ( `id` 100) NULL COMMENT 用户名100) CHARACTER SET latin1 邮箱255) 密码35 Records of test_userINTO `test_user` 23zxjzzz@qq.comimages/avatar.jpg9db06bcff9248837f86d1a6bcf41c9e7154760723929'',1)">haha@haha.com154760874130zhaoxiaojuan@test.com1547608915311547609026321547609076root1547609167341547609206');
模型层
application/common/model/Base.PHP
<?PHP namespace app\common\model; use think\Model; class Base extends Model{ protected $autoWriteTimestamp = true; $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(); return $this->id; } }
application/common/model/Topic.PHP
<?class Topic Base { //查询属于哪个用户 function user() { 连接topic表中的user_id $this->hasOne('User','id','user_id'); } 获取所有留言 static getTopics() { $topics = model("Topic") ->order("id","desc") ->select(); $topics; } 获取某条留言 function getTopic($id) { $topic = self::find(['id' => ]); print_r(model("Topic")->getLastsql());die(); $topic; } }
application/common/model/User.PHP
<? think\Model; class User Base{ 查询账号 function isUser($condition) { $this->where() ->limit(1) ->select(); } }
控制器层
application/index/controller/Base.PHP
<?PHP namespace app\index\controller; think\Controller; Controller { * * 空操作 * @auth singwa * @param [type] $name [description] * @return [type] [description] function _empty($name; } }
application/index/controller/Index.PHP
<? think\Db; app\common\model\User; use app\common\model\Topic as TopicModel; class Index index() { 获取session $name=session('name'获取留言列表 $topic=model('Topic'); $topics=$topic->getTopics(); 输出到模板 echo $this->fetch('',[ 'name'=>$name,'topics'=>$topics,1)"> ]); } 注册 register() { fetch(); } 添加账号 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'=>$email,'password'=>$password,1)"> ]; 存储数据 $user=model('User'$id=$user->add($this->success('注册成功','index/login'); } } 登录 login() { 检测是否登录 if(session('name')){ $this->error('您已经登录',url('index/index')); } fetch(); 输出到头部模板 $this->fetch('common/header',1)">登录检测 isUser(){ $login=$userdata['login'查询条件 $condition1=$login,1)"> ]; $condition2=[ 'email'=> ]; 查询 $login1=$user->isUser($condition1$login2=$condition2); $login1)){ 姓名登录 session('id',$login1[0]->id); session('name',1)">name); $this->success('登录成功','index/index'); }else $login2邮箱登录 session('id',1)">$login2[0]->else{ $this->error('登录失败',1)">); } } } 退出 logout(){ session('name',1)">null$this->redirect('index/index'); } }
application/index/controller/Topic.PHP
<? _initialize(){ if(!session('name')){ $this->error('您没有登录',url('index/login')); } } 发帖页面 new_topic(){ 添加新帖 add_topic(){ $topicdata=input('param.'$title=$topicdata['title'$content=$topicdata['content'$user_id=session('id'); [ 'title'=>$title,'content'=>$content,'user_id'=>$user_id,1)">$topic->add($this->success('添加成功',1)">编辑新帖页面 detail_topic(){ 接收id $id=input('param.id'查询这条帖子的发布者 $condition=[ 'id'=>$id,1)"> ]; $user=model('Topic')->where($condition)->find(); $user_id=$user->user_id; 判断是否有权限编辑帖子 if(session('id') != $user_id){ $this->error('您没有权限修改!'); } $topic=model('Topic')->getTopic([ 'topic'=>$topic,1)"> ]); 更新帖子数据 update_topic(){ $topicdata['id']; [ 'id'=> ]; $res=model('Topic')->where($condition)->update($res$this->success('编辑成功',1)">$this->error('编辑失败'); } } } }
视图层
application/index/view/common/header.html
<!DOCTYPE html> <html lang="en"head> Meta charset="UTF-8"name="viewport" content="width=device-width,initial-scale=1.0"/> title>test论坛</link rel="stylesheet" href="<?=STATIC_URL?>css/all-df86af5803.css"script src="<?=STATIC_URL?>js/all-17476e6cc3.js"></scriptbody class="forum" data-page="forum"div ="header"> nav ="navbar navbar-inverse navbar-fixed-top navbar-default"> ="container"> ="navbar-header" id="navbar-header"> a href="<?=url('index/index');?>" class="navbar-brand"><img ="<?=STATIC_URL;?>images/logo.png"adivid="main-nav-menu"ul ="nav navbar-nav"> li ="active"> ="<?=url('index/index');?>"> i ="fa fa-home"ispan ="hidden-xs hidden-sm">首页spanliul="nav navbar-nav navbar-right"<?PHP if(!isset($name)): ?> ="<?=url('index/register');?>"="signup-btn">注册="<?=url('index/login');?>"="login-btn">登录PHP else: ="<?=url('topic/new_topic');?>">发帖="nav user-bar navbar-nav navbar-right"="dropdown"> ="#"="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">PHP echo $name; ?> ="caret"button ="navbar-toggle" type> ="sr-only">Toggle="fa fa-reorder"button="dropdown-menu"="menu"> ='divider'> ="<?=url('index/logout');?>" onclick="return confirm('你确定要退出吗?')"="fa fa-sign-out">退出> PHP endif; ?> nav>
application/index/view/common/footer.html
footer ="footer"="copyright"blockquote ="pull-left"p>Copyright © 2015-2016 ="#">test> 1.0.0-rc2 ="pipe">|blockquote="pull-right hidden-sm hidden-xs"target="_blank"="<?=STATIC_URL;?>images/test-logo.png" border="0" height="40"="tooltip" data-placement="top" title="Powered by test"/> footerbodyhtml>
application/index/view/common/topic_list.html
="col-md-9 threads-index main-col"="panel panel-default"="panel-heading"="pull-left hidden-sm hidden-xs"="fa fa-list" 首页 ="clearfix"="panel-body remove-padding-horizontal"PHP foreach($topics as $vo): ="list-group row thread-list"="list-group-item media " style="margin-top: 0px;"="avatar pull-left"="<?=url('topic/detail_topic');?>?id=<?PHP echo $vo['id']; ?>"> ="media-object img-thumbnail avatar-48" alt="imooc" src="<?=STATIC_URL;?><?PHP echo $vo['user']['avatar']; ?>"/> ="infos"="media-heading"=""PHP echo $vo['title']; ?>="media-body Meta"> PHP echo $vo['content']; > ="timeago" titlePHP endforeach; ALTER TABLE test_user ALTER avatar SET DEFAULT "images/avatar.jpg" ;
application/index/view/index/index.html
PHP require(APP_PATH.'/index/view/common/header.html');?> ="main"="main-container container"<!-- 首页通栏(4个推荐位) --> PHP require(APP_PATH.'/index/view/common/topic_list.html');PHP require(APP_PATH.'/index/view/common/footer.html');?>
application/index/view/index/login.html
="container-fluid"="row"="col-md-5 col-md-offset-2"="panel-body"form role="form" method="POST" action="<?=url('index/isUser');?>"input type="hidden" name="_token" value="BAnCy9yzQxp30BiIkeU58OMjR5uWeDM9dCSMNwKH"="form-group"="login"="form-control"="" placeholder="用户名或邮箱地址"="password"="密码"="submit"="commit"="登录"="btn btn-primary btn-lg btn-block"form="panel-footer"="<?=url('index/register');?>"<a href="/password/email">忘记密码?</a>--> ="col-md-3">登陆提示="list-group"="list-group-item" 如果您忘记了密码,请联系管理员。 > ?>
application/index/view/index/register.html
="col-md-6 col-md-offset-3"="<?=url('index/add');?>"="text"="username"="用户名"="email"="邮箱地址"="password_confirmation"="确认密码"="btn btn-primary" 注册 ="/"="btn btn-default">取消 已注册或使用社交账号登录,请点击 ="<?=url('index/login');?>">这里 进行登录。 ?>
application/index/view/topic/detail_topic.html
="row max-width"="col-xs-12"="breadcrumb"="index.html"> strong>发表新帖="thread_create"="col-md-9 main-col"="panel panel-default corner-radius"="reply-Box form Box-block"> method="<?=url('topic/update_topic');?>" accept-charset="UTF-8"="thread_create_form"="create_form"> ="thread_title"="标题"="title"="<?= $topic->title?>"="id"="<?= $topic->id?>" editor start --> ="editor-toolbar"="nav nav-pills"="clear:none;"="edit active">帖子内容: end textarea ="post-editor form-control" rows="15" style="overflow:hidden"="body_field"="请撰写内容"="content" cols="50" = $topic->content textarea="form-group status-post-submit"="btn btn-primary col-xs-2"="发布"> ="col-md-3 side-bar"> ="panel panel-default corner-radius help-Box"="panel-heading text-center"h3 ="panel-title">发帖提示h3="list"主题标题 >请在标题中描述内容要点。选择节点 >请为你的主题选择一个节点。恰当的归类会让你发布的信息更有用。正文 >imooc 支持 style="font-family: Consolas,'Panic Sans',mono"="https://help.github.com/articles/github-flavored-markdown" target>GitHub Flavored Markdown> 文本标记语法。你可以在页面下方实时预览正文的实际渲染效果。> >社区指导原则尊重原创 >请不要在 imooc 发布任何盗版链接,包括软件、音乐、电影等。友好互助 >保持对陌生人的友善。用知识去帮助别人。> ?>
application/index/view/topic/new_topic.html
="<?=url('topic/add_topic');?>"="text">帖子内容="50"?>