Classical Software Design in a Web Context
The SOLID principles (Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion) were defined by Uncle Bob Martin decades ago. Surprisingly, they map perfectly to modern REST API design.
Single Responsibility Principle (SRP)
An API endpoint should do one thing. A `POST /users` endpoint should create a user. It should not also send a welcome email, subscribe them to a newsletter, and calculate their horoscope. Delegate those side effects to an event listener. This makes the endpoint fast and robust.
Open/Closed Principle
Your API should be open for extension but closed for modification. This effectively means: You can add new fields to a JSON response (extension), but you cannot rename or remove existing fields (modification) because that would break existing clients.
Dependency Inversion Principle (DIP)
Your Controllers should not depend on concrete classes (like `MySQLDatabase`). They should depend on interfaces (like `UserRepository`).
// Bad
class UserController {
public function store() {
$db = new MySQLDatabase(); // Tight coupling
}
}
// Good
class UserController {
public function __construct(UserRepository $users) { // Dependency Injection
$this->users = $users;
}
}
This allows you to swap MySQL for MongoDB or a widespread Cache layer without changing a single line of your Controller code.
Conclusion
Code that follows SOLID principles is boring. And in software engineering, boring is good. It means predictable, testable, and maintainable.