Lab 15: Capstone — Microservice

Time: 40 minutes | Level: Advanced | Docker: docker run -it --rm php:8.3-cli bash

Build a complete PHP 8.3 microservice combining: Slim 4 (PSR-7/PSR-15 routing), JWT authentication, SQLite persistence, PSR-14 events, and PHPUnit tests. Everything runs in Docker.


Step 1: Project Structure

microservice/
├── composer.json
├── public/
│   └── index.php          # Slim app entry point
├── src/
│   ├── Domain/
│   │   ├── User.php       # readonly class
│   │   └── UserCreatedEvent.php
│   ├── Repository/
│   │   └── UserRepository.php
│   ├── Service/
│   │   ├── AuthService.php
│   │   └── UserService.php
│   ├── Middleware/
│   │   ├── JwtMiddleware.php
│   │   └── JsonResponseMiddleware.php
│   └── Handler/
│       ├── RegisterHandler.php
│       ├── LoginHandler.php
│       └── UserListHandler.php
└── tests/
    ├── Unit/
    │   └── UserServiceTest.php
    └── Integration/
        └── ApiTest.php

Step 2: composer.json & Dependencies

📸 Verified Output:


Step 3: Domain Layer


Step 4: Repository


Step 5: Auth Service with JWT


Step 6: PSR-14 Event System Integration


Step 7: Slim 4 Application


Step 8: Tests & Full Verification

Full test + demo run:

📸 Verified Output:


Running the Full Microservice

📸 Verified Output:


Capstone Architecture Summary


Complete Feature Checklist

Feature
Implementation
Status

Slim 4 routing

PSR-7/PSR-15

JWT auth

firebase/php-jwt

Password hashing

Argon2id

SQLite persistence

PDO + prepared statements

Readonly domain objects

PHP 8.2 readonly class

PSR-14 events

Custom dispatcher + listeners

Middleware pipeline

PSR-15 MiddlewareInterface

PHPUnit tests

Data providers + mocks

Attribute-based attributes

#[Route] pattern

CORS headers

CorsMiddleware

Error handling

Slim error middleware

JSON responses

Content-Type negotiation


Summary

This capstone integrates concepts from all 15 labs:

Lab
Concept Used

Lab 02

readonly class User domain objects

Lab 03

Named arguments, first-class callables

Lab 06

Reflection for DI container patterns

Lab 07

#[Attribute] for route/validation metadata

Lab 10

Argon2id passwords, JWT security

Lab 12

PHPUnit test suite with mocks

Lab 13

PSR-14 event dispatcher

Lab 14

PSR-7/PSR-15 HTTP layer

Last updated