用mvc架构写一个评论功能
时间: 2023-07-04 07:06:49 浏览: 63
好的,下面是使用MVC架构实现评论功能的示例代码:
1. 首先,我们需要创建一个 CommentModel 类,用于存储评论信息。
```php
class CommentModel
{
private $id;
private $content;
private $user_id;
private $post_id;
private $created_at;
public function __construct($id, $content, $user_id, $post_id, $created_at)
{
$this->id = $id;
$this->content = $content;
$this->user_id = $user_id;
$this->post_id = $post_id;
$this->created_at = $created_at;
}
public function getId()
{
return $this->id;
}
public function getContent()
{
return $this->content;
}
public function getUserId()
{
return $this->user_id;
}
public function getPostId()
{
return $this->post_id;
}
public function getCreatedAt()
{
return $this->created_at;
}
}
```
2. 创建 CommentDao 类,用于处理与评论相关的数据库操作。
```php
class CommentDao
{
private $db;
public function __construct($db)
{
$this->db = $db;
}
public function addComment($comment)
{
$stmt = $this->db->prepare('INSERT INTO comments (content, user_id, post_id, created_at) VALUES (?, ?, ?, ?)');
$stmt->execute([$comment->getContent(), $comment->getUserId(), $comment->getPostId(), $comment->getCreatedAt()]);
$comment->setId($this->db->lastInsertId());
}
public function getCommentsByPostId($post_id)
{
$stmt = $this->db->prepare('SELECT * FROM comments WHERE post_id = ?');
$stmt->execute([$post_id]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$comments = [];
foreach ($rows as $row) {
$comment = new CommentModel($row['id'], $row['content'], $row['user_id'], $row['post_id'], $row['created_at']);
$comments[] = $comment;
}
return $comments;
}
}
```
3. 创建 CommentController 类,用于处理与评论相关的业务逻辑。
```php
class CommentController
{
private $dao;
public function __construct($dao)
{
$this->dao = $dao;
}
public function addComment($content, $user_id, $post_id)
{
$created_at = date('Y-m-d H:i:s');
$comment = new CommentModel(null, $content, $user_id, $post_id, $created_at);
$this->dao->addComment($comment);
}
public function getCommentsByPostId($post_id)
{
return $this->dao->getCommentsByPostId($post_id);
}
}
```
4. 创建 CommentView 类,用于显示评论列表和添加评论的表单。
```php
class CommentView
{
public function renderComments($comments)
{
$html = '<ul>';
foreach ($comments as $comment) {
$html .= '<li>' . $comment->getContent() . '</li>';
}
$html .= '</ul>';
echo $html;
}
public function renderCommentForm($post_id)
{
$html = '<form method="POST" action="/add-comment">';
$html .= '<input type="hidden" name="post_id" value="' . $post_id . '">';
$html .= '<label for="content">Comment:</label>';
$html .= '<textarea name="content"></textarea>';
$html .= '<button type="submit">Submit</button>';
$html .= '</form>';
echo $html;
}
}
```
5. 最后,创建一个 index.php 文件,用于处理 HTTP 请求并渲染页面。
```php
$db = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password');
$dao = new CommentDao($db);
$controller = new CommentController($dao);
$view = new CommentView();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['post_id']) && isset($_POST['content'])) {
$controller->addComment($_POST['content'], 1, $_POST['post_id']); // 假设当前用户 ID 为 1
}
$post_id = 1; // 假设当前页面为文章 ID 为 1 的页面
$comments = $controller->getCommentsByPostId($post_id);
$view->renderComments($comments);
$view->renderCommentForm($post_id);
```
以上就是使用 MVC 架构实现评论功能的示例代码,你可以根据需要进行修改和扩展。