Detailed explanation of php forum development steps

PHPz
Release: 2023-04-04 15:22:01
Original
810 people have browsed it

PHP is a popular server-side scripting language that is widely used in website development. Among them, forum is a classic application scenario, so developing a forum system based on PHP is a very meaningful task. This article will introduce the steps of php forum development to help developers get started quickly.

  1. Design database model

A good forum should have functions such as posts, replies, users, etc. Therefore, we need to design a database model to support the implementation of these functions. A simple model design is as follows:

  • User table: id (primary key), username, password, email, created_at
  • Post table: id (primary key), user_id (foreign key) , title, content, created_at, updated_at
  • Reply table: id (primary key), user_id (foreign key), post_id (foreign key), content, created_at, updated_at

Among them, The user table saves basic user information and has a related field for associating posts and reply tables; the posts table is used to save topic posts, which includes title, content, author and other information; the reply table is used to save reply content, where Contains information such as the person who replied, the content of the reply, and the topic post of the reply.

  1. Writing database script

After designing the database model, we need to create the corresponding table structure. In the MySQL database, you can use sql statements to create table structures.

For convenience, we can first create a database named forum in phpMyAdmin, and then create three tables in it. The specific sql statement is as follows:

  • User table:
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;
Copy after login
  • Post table:
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;
Copy after login
  • Reply table:
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;
Copy after login
  1. Writing the configuration file

Before writing the php code, we need to configure the database connection information first. A simple configuration file is used here to accomplish this task. The specific code is as follows:

// config.php

define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'forum');
Copy after login

This file defines the information required for database connection, including database host address, user name and password, and database name.

  1. Write php code

Before writing php code, we need to install the necessary dependent libraries. Here we use composer to complete the installation of dependent libraries. The specific process is as follows:

  • Enter composer init in the terminal or command line to initialize a new composer project;
  • Edit the composer.json file and add the following dependencies:
{
  "require": {
    "slim/slim": "^4.5",
    "illuminate/database": "^8.0"
  }
}
Copy after login

Here we use two dependency libraries, slim and illuminate/database, for routing management and database calls. . Among them, slim is a lightweight php framework that can easily implement RESTful interfaces; illuminate/database is the database ORM module used in the Laravel framework, providing a very rich database operation method.

  • Execute composer install in the terminal or command line to install dependencies.

After completing the installation of dependent libraries, we can start writing php code. Here, we will name it index.php to handle various requests from the forum.

<?php

require &#39;vendor/autoload.php&#39;;
require &#39;config.php&#39;;

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();
Copy after login

In the code, we use the slim framework to handle routing requests and illuminate/database to handle the database.

  1. Writing front-end code

After completing the server-side code, we also need to write the front-end page code. Here, we can use the bootstrap framework to accomplish this. The specific code is as follows:

<!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>
Copy after login
  1. Test the operation of the forum

After completing the writing of php and front-end code, we can finally test the operation of the forum. First, we need to run the following command in the terminal or command line to start the server:

php -S localhost:8080 index.php
Copy after login

Then, enter http://localhost:8080 in the browser to access the forum homepage.

At this point, we have completed the development of the forum system based on PHP. In this process, we introduced the complete process of database model design, database script writing, PHP code writing and front-end page design, hoping to be helpful to the majority of developers.

The above is the detailed content of Detailed explanation of php forum development steps. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!