πŸ“– Building a Blog

Why We Use It

Adding a blog helps your team demonstrate full-stack capability: database design, content rendering, controller routing, and page templating. The blog system can be extended later with pagination, categories, or user comments, but this guide focuses on the foundation.

The blog includes two primary views for visitors:

  • views/blog/index.php – displays all published blog posts
  • views/blog/show.php – displays a single post selected by ID

Each blog post includes a title, body, publish_date, and an author_id for attribution.

Project Structure

The blog follows the same MVC folder pattern as the user profile system. These files are required for the public-facing feature:

project-root/
β”œβ”€β”€ controllers/
β”‚   └── PostController.php        ← handles public blog routing
β”œβ”€β”€ models/
β”‚   └── PostModel.php             ← queries for blog data
β”œβ”€β”€ views/
β”‚   └── blog/
β”‚       β”œβ”€β”€ index.php             ← shows all posts
β”‚       └── show.php              ← shows single post
β”œβ”€β”€ index.php                     ← public entry point (loads blog index)

Database Setup

Create the posts table and add a few starter records:

-- Create the posts table
CREATE TABLE posts (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  body TEXT NOT NULL,
  publish_date DATE NOT NULL,
  author_id INT NOT NULL
);

-- Insert sample posts
INSERT INTO posts (title, body, publish_date, author_id) VALUES
('Welcome to Our Blog', 'This is the first post on our company blog!', '2025-01-01', 1),
('Spring Product Update', 'See what’s new in our spring release.', '2025-04-15', 1),
('Industry News Roundup', 'A quick look at recent tech news.', '2025-06-05', 2);

How It Works

PostModel Methods

// PostModel.php

public static function getAllPosts() {
  global $pdo;
  $stmt = $pdo->prepare("SELECT * FROM posts ORDER BY publish_date DESC");
  $stmt->execute();
  return $stmt->fetchAll();
}

public static function getPostById($id) {
  global $pdo;
  $stmt = $pdo->prepare("SELECT * FROM posts WHERE id = ?");
  $stmt->execute([$id]);
  return $stmt->fetch();
}

PostController Routing

The blog controller provides methods to load views based on query parameters:

// PostController.php

class PostController {
  public static function index() {
    $posts = PostModel::getAllPosts();
    require 'views/blog/index.php';
  }

  public static function show() {
    $id = $_GET['id'] ?? null;
    $post = $id ? PostModel::getPostById($id) : null;
    if ($post) {
      require 'views/blog/show.php';
    } else {
      echo "<p>Post not found.</p>";
    }
  }
}

Entry Point

The main index.php page routes users to the blog list or individual post view:

<?php
require_once 'controllers/PostController.php';

if (isset($_GET['id'])) {
  PostController::show();
} else {
  PostController::index();
}

Common Issues

  • Posts not showing up? Make sure your database table is populated and the connection is correct.
  • Missing title or body? Sanitize and validate your database input during creation.
  • HTML not rendering correctly? Escape output selectively with htmlspecialchars() where needed.

Team Guidelines

  • Use consistent formatting for post display (headlines, dates, spacing)
  • Avoid inline styles in views β€” use CSS classes from the site template
  • Name controller methods clearly (e.g., index(), show())
  • Add author logic in a future enhancement β€” use a join on users.id = posts.author_id

Summary / Takeaways

  • The blog is built using the same MVC pattern as other app features
  • Only index.php, PostController.php, and PostModel.php are needed for read-only functionality
  • Blog views should be lightweight, secure, and mobile-friendly

Additional Resources

Last updated: August 9, 2025 at 10:52 PM