thinkphp論壇項目開發

来源:https://www.cnblogs.com/chenyingying0/archive/2020/01/17/12205495.html
-Advertisement-
Play Games

效果圖 首先是資料庫 /* 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 &copy;
              
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ©Copyright 蕃薯耀 2020-01-17 https://www.cnblogs.com/fanshuyao/ 具體的方法如下: /** * 把字元串數字類型的數字取出來(只取遇到非數字字元前,包括空格) * @param str * <li>"1-0我5013我24a5c6" 》 1</ ...
  • 你可以訪問 "碼雲 樂優商城" 來獲取關於樂優商城的工程代碼。 你可以訪問 "百度雲 樂優優商城" 密碼:ppzy 來獲取關於樂優商城的資料。 一、創建父工程 Maven Project 用來管理依賴 GroupId:項目中唯一標識符,對應的是java中的包結構,在這裡表示項目中的結構 Artifa ...
  • 之前我們用SSM或者SSH進行JAVA WEB開發的時候,IDEA 需要配置Tomcat然後把項目放到tomcat運行,tomcat啟動的時候會自動打開瀏覽器去訪問項目,但是SpringBoot是內嵌tomcat的,項目啟動成功後無法自主訪問,需要我們手動打開瀏覽器輸入url訪問,我覺得這樣很不習慣... ...
  • 前言 Golang 目前的主要應用領域還是後臺微服務,雖然在業務領域也有所應用但仍然是比較小衆的選擇。大多數的服務運行環境都是linux,而在windows中golang應用更少,而作者因爲特殊情況,不得已要在widows環境中用golang去寫本地代理服務。在我的使用場景中實時性要求非常高(視頻通 ...
  • import java.io.*; public class test13_6 { public static void main(String []args) throws Exception { FileOutputStream output=null; FileInputStream inpu ...
  • 原文:https://www.jianshu.com/p/e88d3f8151db JWT官網: https://jwt.io/ JWT(Java版)的github地址:https://github.com/jwtk/jjwt 什麼是JWT Json web token (JWT), 是為了在網路應 ...
  • 一、Unittest 單元測試框架,可用於自動化測試用力組織,執行,輸出結果 二、Unittest構成 1. Test Case 2. Test Suite 3. Test Fixture 4. Test Runner (圖片來源於網路) Test Case 一個測試用例是一個獨立的測試單元。它檢查 ...
  • 你可以訪問 "碼雲 樂優商城" 來獲取關於樂優商城的工程代碼。 你可以訪問 "百度雲 樂優優商城" 密碼:ppzy 來獲取關於樂優商城的資料。 一、介紹 樂優商城是一個全品類的電商購物網站(B2C),作為階段性學習工程。 二、軟體架構 三、前端技術選型 1. 基礎的HTML、CSS、JavaScri ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...