About this chapter
Chapters 3 and 4 taught you to model the business - the language, the boundaries, and the building blocks like entities, aggregates and repositories. This chapter answers a different question: where does all that code physically sit, and what is it allowed to depend on? Hexagonal architecture (also called ports and adapters) gives one clear answer. The domain goes in the center, everything technical stays outside, and the two meet only through interfaces called ports. You'll learn what a port and an adapter are, the difference between the code that drives your app and the code your app drives, and how the same idea shows up under the names Onion and Clean architecture. The chapter ends with vertical slice architecture, an honest alternative that organizes by feature instead of by layer.
Lessons from courses
- 1 What is hexagonal architecture? Hexagonal architecture (ports and adapters) puts the domain in the center, isolated from the database, HTTP and the framework. A PHP beginner's guide.
- 2 The domain at the center The dependency rule of hexagonal architecture: all dependencies point inward, and the domain knows nothing about the database, HTTP or the framework.
- 3 Ports A port is an interface the domain defines: driven ports for what it needs (OrderRepository) and driving ports for what it offers. With a PHP 8.4 example.
- 4 Adapters An adapter implements a port: a MysqlOrderRepository or a controller. Swap adapters without touching the domain. Ports and adapters explained in PHP.
- 5 Driving vs driven adapters Primary/driving adapters call the app (HTTP, CLI, tests); secondary/driven adapters are called by the app (DB, mail, queue). The flow of control explained.
- 6 Hexagonal vs onion vs clean architecture Hexagonal, onion and clean architecture are three names for one idea: dependencies point inward and the domain stays isolated. The differences and shared core.
- 7 Vertical slice architecture Vertical slice architecture organizes code by feature, not by layer: a full slice from request to persistence. When it beats layered or hexagonal, honestly.