Skip to main content

Lesson 7 of 7

Mini Project: Blog System - Part 5 (Interfaces & SOLID Architecture)

Restructure the Blog System around interfaces, PSR-4 namespaces, Composer autoloading, and SOLID principles. Introduce the full layered architecture: Domain, Repository, Service, and HTTP layers with proper dependency injection.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Blog Part 5 - Full SOLID Architecture

Blog Part 5 - Full SOLID Architecture
<?php
// ── Directory structure ───────────────────────────────────────
// src/
// ├── Blog/
// │   ├── Domain/                    ← Entities, Value Objects, Enums
// │   │   ├── Post.php
// │   │   ├── Author.php
// │   │   ├── PostStatus.php         (Backed Enum)
// │   │   └── Email.php              (Value Object)
// │   ├── Repository/                ← Repository Interfaces
// │   │   └── PostRepositoryInterface.php
// │   └── Service/                   ← Business Logic
// │       └── PostService.php
// ├── Infrastructure/
// │   └── Repository/                ← Concrete Implementations
// │       ├── PdoPostRepository.php
// │       └── InMemoryPostRepository.php
// ├── Http/
// │   └── Controller/
// │       └── PostController.php
// └── Bootstrap/
//     └── Container.php             ← Manual DI container

// ── src/Blog/Domain/PostStatus.php ───────────────────────────
namespace App\Blog\Domain;

enum PostStatus: string {
    case Draft     = "draft";
    case Published = "published";
    case Archived  = "archived";

    public function canBePublished(): bool { return $this === self::Draft; }
    public function label(): string { return ucfirst($this->value); }
}

// ── src/Blog/Repository/PostRepositoryInterface.php ──────────
namespace App\Blog\Repository;
use App\Blog\Domain\Post;

interface PostRepositoryInterface {
    public function findById(int $id): ?Post;
    public function findAllPublished(int $limit = 20, int $offset = 0): array;
    public function save(Post $post): Post;
    public function delete(int $id): bool;
}

// ── src/Blog/Service/PostService.php ─────────────────────────
namespace App\Blog\Service;
use App\Blog\Domain\{Post, PostStatus};
use App\Blog\Repository\PostRepositoryInterface;

class PostService {
    public function __construct(
        private readonly PostRepositoryInterface $posts,
    ) {}

    public function publish(int $id): Post {
        $post = $this->posts->findById($id);
        if ($post === null) throw new \RuntimeException("Post $id not found.");
        if (!$post->getStatus()->canBePublished()) {
            throw new \LogicException("Post cannot be published from '{$post->getStatus()->label()}' state.");
        }
        return $this->posts->save($post->publish());
    }

    public function listPublished(int $page = 1, int $perPage = 10): array {
        return $this->posts->findAllPublished($perPage, ($page - 1) * $perPage);
    }
}

// ── src/Bootstrap/Container.php - Simple DI Container ────────
namespace App\Bootstrap;
use PDO;
use App\Blog\Repository\PostRepositoryInterface;
use App\Blog\Service\PostService;
use App\Infrastructure\Repository\PdoPostRepository;

class Container {
    private array $bindings = [];

    public function __construct() {
        $this->register();
    }

    private function register(): void {
        $pdo = new PDO(
            dsn:      $_ENV["DB_DSN"]      ?? "mysql:host=localhost;dbname=blog;charset=utf8mb4",
            username: $_ENV["DB_USER"]     ?? "root",
            password: $_ENV["DB_PASSWORD"] ?? "",
            options: [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]
        );

        $this->bindings[PostRepositoryInterface::class] = new PdoPostRepository($pdo);
        $this->bindings[PostService::class] = new PostService(
            $this->bindings[PostRepositoryInterface::class]
        );
    }

    public function get(string $class): mixed {
        return $this->bindings[$class]
            ?? throw new \RuntimeException("No binding for $class");
    }
}

// ── public/index.php ──────────────────────────────────────────
require __DIR__ . "/../vendor/autoload.php";

use App\Bootstrap\Container;
use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__ . "/../");
$dotenv->load();

$container   = new Container();
$postService = $container->get(PostService::class);
$posts       = $postService->listPublished(page: 1, perPage: 10);

header("Content-Type: application/json");
echo json_encode(["data" => array_map(fn($p) => $p->toArray(), $posts)]);

Quick Quiz

Module Quiz

Module Quiz
Question 1 of 5

What is the key difference between an interface and an abstract class in PHP?

Tip

Tip

Practice Mini Project Blog System Part 5 Interfaces SOLID Architecture in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Mini Project: Blog System - Part 5 (Interfaces & SOLID Architecture)

Practice Task

Note

Practice Task - (1) Write a working example of Mini Project Blog System Part 5 Interfaces SOLID Architecture 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 5 Interfaces SOLID Architecture is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/7 done