Lesson 8 of 8
Mini Project: Blog System - Part 3 (Contact Form & PRG)
Extend the Blog System with a complete contact form using the Validator class, PRG pattern, flash messages via sessions, and an image upload for the post feature - all wired together in a proper front-controller router.
Blog Part 3 - Full Request Handling
<?php
declare(strict_types=1);
// public/index.php - Front controller router
ini_set("session.cookie_httponly", "1");
ini_set("session.cookie_secure", "1");
ini_set("session.cookie_samesite", "Lax");
session_start();
$config = require __DIR__ . "/../config/app.php";
require_once __DIR__ . "/../src/helpers.php";
require_once __DIR__ . "/../src/Validator.php";
// Parse route
$uri = trim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), "/");
$method = $_SERVER["REQUEST_METHOD"];
// ── Router dispatch table ──────────────────────────────────────
$routes = [
"" => ["GET" => "home"],
"contact" => ["GET" => "contact_show", "POST" => "contact_store"],
"posts" => ["GET" => "posts_index"],
];
// Extract first segment for simple matching
$segment = explode("/", $uri)[0];
$route = $routes[$segment] ?? null;
if ($route === null || !isset($route[$method])) {
http_response_code(404);
include __DIR__ . "/../views/404.php";
exit;
}
$action = $route[$method];
// ── Controllers inline (move to separate files as project grows) ──
match ($action) {
"home" => home(),
"contact_show" => contactShow(),
"contact_store" => contactStore(),
"posts_index" => postsIndex(),
default => fn() => null,
};
function home(): void {
include __DIR__ . "/../views/home.php";
}
function contactShow(): void {
$flashSuccess = getFlashMessages("success");
$flashErrors = getFlashMessages("errors");
$old = getFlashMessages("old")[0] ?? [];
include __DIR__ . "/../views/contact.php";
}
function contactStore(): void {
$v = new Validator();
$v->required("name", $_POST["name"] ?? "", "Name")
->minLength("name", $_POST["name"] ?? "", 2, "Name")
->required("email", $_POST["email"] ?? "", "Email")
->email("email", $_POST["email"] ?? "")
->required("message", $_POST["message"] ?? "", "Message")
->minLength("message", $_POST["message"] ?? "", 10, "Message")
->maxLength("message", $_POST["message"] ?? "", 2000, "Message");
if ($v->fails()) {
// Flash errors and old input - then redirect back to form (PRG)
$_SESSION["flash"]["errors"][] = $v->errors();
$_SESSION["flash"]["old"][] = $_POST;
header("Location: /contact");
exit;
}
// Process (send email, save to DB, etc.)
// sendContactEmail(...);
flashMessage("success", "Thank you! Your message was sent.");
header("Location: /contact");
exit;
}
function postsIndex(): void {
require_once __DIR__ . "/../src/PostCollection.php";
$postsData = require __DIR__ . "/../data/posts.php";
$collection = new PostCollection($postsData);
$page = max(1, (int) ($_GET["page"] ?? 1));
$search = trim($_GET["search"] ?? "");
$results = $search
? $collection->published()->search($search)
: $collection->published()->sortBy("created_at", "desc");
$paginated = $results->paginate(5, $page);
include __DIR__ . "/../views/posts/index.php";
}Quick Quiz
Module Quiz
What does the PRG (Post/Redirect/Get) pattern prevent?
Tip
Tip
Practice Mini Project Blog System Part 3 Contact Form PRG in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Mini Project: Blog System - Part 3 (Contact Form & PRG)
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Blog System Part 3 Contact Form PRG from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Mini Project Blog System Part 3 Contact Form PRG is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.
Finished this lesson?