Clean Code in Laravel
A practical guide to writing maintainable, readable Laravel applications using PHP 8.5 and Laravel 13 — and to keeping them clean when AI writes half the code.
Table of Contents
Introduction
Part 1 — Think Clean
- The Philosophy of Simplicity
- Naming Conventions
- Code Quality and Automation
- Dependency Injection
Part 2 — Write Clean
- Controllers
- Actions
- Data Transfer Objects
- Organizing Your Application
- Form Requests and Validation
- View Models
- Jobs
- Pipelines
- APIs
- Webhooks
- Rate Limiting
- Authorization
Part 3 — Model Clean
- Eloquent Models Done Right
- Custom Query Builders and Collections
- Enums, Value Objects, and Type Safety
- The State Pattern
- Database Best Practices
Part 4 — Ship Clean
- The Art of Testing
- Queue Workers
- Packages
- Security
- Deployments
- AI Observability with Spectra
Bonus — Clean Code in the AI Era
- Why Clean Code Still Matters
- Teaching AI Your Conventions
Afterword
Use the Book With Your AI Agents
Every convention in this book is available as instructions for AI coding agents.
AI rules — install them into any Laravel project. The installer asks which tools you use (Claude Code and Cursor by default; also GitHub Copilot, Windsurf/Devin, Junie, Codex and other AGENTS.md agents, Gemini, and Laravel Boost) and whether to add the Clean Code in Laravel skill, then writes everything in each tool's format:
curl -fsSL https://mayahi.net/laravel/ai-rules.sh | bash
Skip the questions with flags, for example bash -s -- --agent claude,cursor --skills all. See ai-rules/ for every option.
Claude Code skill — build features, refactor safely, and review code against the book:
/plugin install clean-code-in-laravel --marketplace ahmadmayahi/clean-code-in-laravel
On older Claude Code versions, run /plugin marketplace add ahmadmayahi/clean-code-in-laravel and then /plugin install clean-code-in-laravel@clean-code-in-laravel. Then ask Claude to /clean-code-in-laravel review app/Http/Controllers.
About