This project follows the Repository Design Pattern while adhering to the SOLID principles:
-
Single Responsibility Principle (SRP) – Each layer has a distinct role:
- The repository handles database interactions.
- The service manages business logic.
- The controller deals with HTTP requests and validation.
-
Open/Closed Principle (OCP) – Functionality can be extended by adding new services or repositories without modifying existing code.
-
Liskov Substitution Principle (LSP) – The CommonRepository and CommonService allow child classes (e.g., Book, Author, Publisher) to replace them without breaking functionality.
-
Interface Segregation Principle (ISP) – Specific repositories handle logic for their respective models, avoiding a single, overly complex repository.
-
Dependency Inversion Principle (DIP) – Services depend on abstract repositories rather than concrete Eloquent models, enhancing flexibility.
app/
├─ Http/
│ ├─ Controllers/
│ │ ├─ CommonController.php
│ │ ├─ BookController.php
│ │ ├─ AuthorController.php
│ │ └─ PublisherController.php
│ └─ Services/
│ ├─ CommonService.php
│ ├─ BookService.php
│ ├─ AuthorService.php
│ └─ PublisherService.php
├─ Models/
│ ├─ Book.php
│ ├─ Author.php
│ └─ Publisher.php
├─ Repositories/
│ ├─ CommonRepository.php
│ ├─ BookRepository.php
│ ├─ AuthorRepository.php
│ └─ PublisherRepository.php
└─ Traits/
└─ ValidatesData.php
Here's a high-level flow map of how requests move through project:
Client (API request)
│
▼
Routes (routes/api.php)
│
▼
Controller (BookController, AuthorController, etc.)
│
▼
Validation (ValidatesData trait + getRules())
│
▼
Service Layer (BookService, AuthorService, etc.)
│
▼
Repository Layer (BookRepository, AuthorRepository, etc.)
│
▼
Model (Book, Author, Publisher)
│
▼
Database (MySQL/PostgreSQL, etc.)
│
▼
Response back to Client
(Model → Repository → Service → Controller → JSON Response)
- Client makes a request → Hits the correct route (
routes/api.php
). - Controller processes the request → Calls validation (
getRules()
method). - Service applies business logic → Calls the repository.
- Repository interacts with the database → Retrieves or modifies data via Eloquent models.
- Response is sent back → Data passes through service and controller, then returns as JSON.
This structure ensures:
- Separation of concerns (each layer has a specific role).
- Modular & maintainable architecture (easy to update and scale).
- SOLID principles (controllers don’t directly interact with the database).