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:

  1. Controller: receives the request and transforms the data into DTOs.
  2. DTO: validates and structures the data.
  3. Service: applies business rules.
  4. 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:

{
    "token": "jwt_token_here",
    "user": {
        "id": 1,
        "name": "Test"
    }
}

๐Ÿงช Testing

  • Services and Repositories can be tested in isolation.
  • DTOs ensure data consistency in tests.

Run tests:

php artisan test

๐Ÿ–Œ Code Quality

  • Laravel Pint: apply code style formatting.
  • PHPStan Level 8: advanced static analysis to detect issues early.
composer pint
composer stan

๐Ÿ“ฆ Installation

git clone https://github.com/WilsonRU/laravel-rest-boilerplate.git
cd laravel-rest-boilerplate
composer install
cp .env.example .env
php artisan key:generate
php artisan migrate

๐ŸŽฏ 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.