PHP 是一種流行的伺服器端腳本語言,被廣泛應用於網站開發領域。其中,論壇是一個經典的應用場景,因此,開發一個基於 PHP 的論壇系統是一項很有意義的任務。本文將介紹 php 論壇開發步驟,幫助廣大開發者快速上手。
一個好的論壇應該具備貼文、回文、使用者等功能,因此,我們需要設計資料庫模型來支撐這些功能的實作。一個簡單的模型設計如下:
forum 的資料庫,然後在其中建立三張表。具體的sql 語句如下:
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `posts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `title` varchar(255) NOT NULL, `content` text NOT NULL, `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `user_id` (`user_id`), CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `replies` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `post_id` int(11) NOT NULL, `content` text NOT NULL, `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `user_id` (`user_id`), KEY `post_id` (`post_id`), CONSTRAINT `replies_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `replies_ibfk_2` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
// config.php define('DB_HOST', 'localhost'); define('DB_USERNAME', 'root'); define('DB_PASSWORD', ''); define('DB_NAME', 'forum');
初始化一個新的composer 專案;
文件,加入以下依賴項:
{ "require": { "slim/slim": "^4.5", "illuminate/database": "^8.0" } }
安裝依賴項。
index.php,用於處理論壇的各種請求。
<?php require 'vendor/autoload.php'; require 'config.php'; use Illuminate\Database\Capsule\Manager as Capsule; use Slim\Factory\AppFactory; use Slim\Views\PhpRenderer; $capsule = new Capsule; $capsule->addConnection([ 'driver' => 'mysql', 'host' => DB_HOST, 'username' => DB_USERNAME, 'password' => DB_PASSWORD, 'database' => DB_NAME, 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', ]); $capsule->setAsGlobal(); $capsule->bootEloquent(); $renderer = new PhpRenderer('templates/'); $app = AppFactory::create(); $app->get('/', function ($request, $response, $args) use ($renderer) { $posts = Capsule::table('posts') ->select('posts.id', 'posts.title', 'posts.content', 'posts.created_at', 'users.id as user_id', 'users.username') ->leftJoin('users', 'posts.user_id', '=', 'users.id') ->orderBy('posts.created_at', 'desc') ->get(); return $renderer->render($response, 'index.php', [ 'posts' => $posts ]); }); $app->run();
<!DOCTYPE html> <html> <head> <title>论坛首页</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/5.0.0-alpha2/css/bootstrap.min.css"> <script src="https://cdn.bootcss.com/bootstrap/5.0.0-alpha2/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark"> <div class="container"> <a href="#" class="navbar-brand">论坛首页</a> </div> </nav> <div class="container mt-3"> <table class="table table-hover"> <thead> <tr> <th>#</th> <th>标题</th> <th>作者</th> <th>发布时间</th> <th>操作</th> </tr> </thead> <tbody> <?php foreach ($posts as $post): ?> <tr> <td><?= $post->id ?></td> <td><?= $post->title ?></td> <td><?= $post->username ?></td> <td><?= $post->created_at ?></td> <td> <a href="#" class="btn btn-sm btn-outline-secondary">编辑</a> <a href="#" class="btn btn-sm btn-outline-danger">删除</a> </td> </tr> <?php endforeach ?> </tbody> </table> </div> </body> </html>
php -S localhost:8080 index.php
http://localhost:8080 即可存取論壇首頁。
以上是詳解php論壇開發步驟的詳細內容。更多資訊請關注PHP中文網其他相關文章!