How can the MVC (Model-View-Controller) principle be effectively applied when handling comments in a PHP blog application?
Issue: When handling comments in a PHP blog application, the MVC principle can be effectively applied by separating the logic for handling comments into the Model, View, and Controller components. This helps in keeping the code organized, maintainable, and scalable.
// Controller (commentController.php)
class CommentController {
public function addComment($postId, $commentText) {
$commentModel = new CommentModel();
$commentModel->addComment($postId, $commentText);
// Redirect to the post page
}
}
// Model (commentModel.php)
class CommentModel {
public function addComment($postId, $commentText) {
// Logic to add comment to the database
}
}
// View (commentView.php)
// Display comments on the post page