Laravel REST API Boilerplate with Service Layer, DTOs, and Clean Architecture
Aug 27, 2025
This boilerplate provides a clean and scalable foundation for applications built with Laravel 12, incorporating best practices in architecture, separation of responsibilities, and the use of DTOs (Data Transfer Objects).
๐ Key Features
- Laravel 12 โ the latest version of the framework.
- Service Layer โ isolation of business logic from controllers.
- Repository Pattern โ abstraction of persistence, facilitating testing and maintenance.
- Rate Limiter โ protection against abuse and brute-force attacks.
- DTOs (Data Transfer Objects) โ validation and standardization of data transferred between layers.
- Clean Architecture Principles โ ensures long-term scalability and maintainability.
- Laravel Sanctum with JWT โ ready-to-use REST API authentication.
- Laravel Pint โ automatic code formatting.
- PHPStan (Level 8) โ static analysis for type safety and error prevention.
๐ Project Structure
app/
โโโ Dto/ # Data Transfer Objects
โโโ Http/
โ โโโ Controllers/ # Controllers for requests and responses
โโโ Repositories/ # Repository interfaces and implementations
โโโ Services/ # Business logic layer
โโโ Models/ # Eloquent Models
๐ How It Works
The flow follows the pattern Request โ Controller โ DTO โ Service โ Repository โ Database:
- Controller: receives the request and transforms the data into DTOs.
- DTO: validates and structures the data.
- Service: applies business rules.
- Repository: interacts with the database.
This keeps controllers lean and the system modular.
๐ Authentication
The boilerplate includes authentication via Laravel Sanctum + JWT for REST APIs.
Example of a successful login:
๐งช Testing
- Services and Repositories can be tested in isolation.
- DTOs ensure data consistency in tests.
Run tests:
๐ Code Quality
- Laravel Pint: apply code style formatting.
- PHPStan Level 8: advanced static analysis to detect issues early.
๐ฆ Installation
๐ฏ Conclusion
The Laravel REST API Boilerplate is a solid foundation for developing modern APIs, ensuring security, scalability, and code quality. Ideal for those looking to start projects with best practices already implemented.