/laravel-modules-test

Vibe coded project about how to modularized laravel project. Only for learning purposes.

Primary LanguagePHP

Laravel Logo

Build Status Total Downloads Latest Stable Version License

About Laravel

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:

Laravel is accessible, powerful, and provides tools required for large, robust applications.

Learning Laravel

Laravel has the most extensive and thorough documentation and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.

You may also try the Laravel Bootcamp, where you will be guided through building a modern Laravel application from scratch.

If you don't feel like reading, Laracasts can help. Laracasts contains thousands of video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.

Laravel Sponsors

We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel Partners program.

Premium Partners

Contributing

Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the Laravel documentation.

Code of Conduct

In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.

Security Vulnerabilities

If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via taylor@laravel.com. All security vulnerabilities will be promptly addressed.

License

The Laravel framework is open-sourced software licensed under the MIT license.


🏗️ Laravel Modular Template

This project extends Laravel with a feature-based modular architecture using nWidart/laravel-modules for building scalable applications.

🚀 Modular Features

  • Feature-Based Modules: Each module contains everything related to a specific feature (like NestJS)
  • Clean Architecture: Separation of concerns with Controllers, Services, Models, Requests, etc.
  • Auto-Discovery: Modules are automatically discovered and registered
  • Independent Development: Teams can work on modules independently
  • Microservice Ready: Easy to extract modules into separate services later
  • Comprehensive CLI: Rich set of Artisan commands for module management

📁 Module Structure

Each module follows this organized structure:

Modules/
├── Auth/
│   ├── app/
│   │   ├── Http/
│   │   │   ├── Controllers/AuthController.php
│   │   │   └── Requests/
│   │   │       ├── LoginRequest.php
│   │   │       └── RegisterRequest.php
│   │   ├── Models/User.php
│   │   ├── Services/AuthService.php
│   │   ├── Transformers/UserResource.php
│   │   └── Providers/
│   ├── routes/
│   │   ├── api.php
│   │   └── web.php
│   ├── database/migrations/
│   ├── tests/
│   └── module.json
│
└── Product/
    └── [same structure...]

🛠️ Getting Started with Modules

Create a New Module

php artisan module:make ModuleName

Generate Module Components

# Create components for your module
php artisan module:make-model ModelName ModuleName
php artisan module:make-controller ControllerName ModuleName
php artisan module:make-service ServiceName ModuleName
php artisan module:make-request RequestName ModuleName
php artisan module:make-migration create_table_name ModuleName

Module Operations

# List all modules
php artisan module:list

# Enable/Disable modules
php artisan module:enable ModuleName
php artisan module:disable ModuleName

# Run module migrations
php artisan module:migrate ModuleName

🔧 Current Modules

Auth Module

  • Purpose: User authentication and management
  • API Routes:
    • POST /api/v1/auth/login
    • POST /api/v1/auth/register
    • POST /api/v1/auth/logout (protected)
    • GET /api/v1/auth/profile (protected)
  • Features: JWT authentication with Laravel Sanctum

Product Module

  • Purpose: Product management
  • API Routes:
    • GET /api/v1/products - List all products
    • POST /api/v1/products - Create new product
    • GET /api/v1/products/{id} - Get specific product
    • PUT /api/v1/products/{id} - Update product
    • DELETE /api/v1/products/{id} - Delete product

🏗️ Architecture Benefits

1. Feature Isolation

Everything related to one feature lives in one place:

  • Controllers handle HTTP requests
  • Services contain business logic
  • Models define data structure
  • Requests validate input
  • Resources format API responses

2. Clean Dependencies

// Example: AuthController with dependency injection
class AuthController extends Controller
{
    public function __construct(
        private AuthService $authService
    ) {}

    public function login(LoginRequest $request): JsonResponse
    {
        $user = $this->authService->login($request->validated());
        return response()->json([
            'user' => new UserResource($user)
        ]);
    }
}

3. Team Collaboration

  • Team A works on Auth module
  • Team B works on Product module
  • No conflicts or cross-dependencies

🚀 Scaling Your Application

Add More Modules

# Create business-specific modules
php artisan module:make OrderManagement
php artisan module:make PaymentProcessing
php artisan module:make Notification
php artisan module:make Analytics

Module Enhancement

# Add more components to existing modules
php artisan module:make-middleware AuthMiddleware Auth
php artisan module:make-job SendWelcomeEmail Auth
php artisan module:make-event UserRegistered Auth

🔐 Authentication Setup

This template includes Laravel Sanctum for API authentication:

# Sanctum is already installed and configured
# To test authentication, use the provided auth endpoints

📚 Available Module Commands

Run php artisan list module to see all 40+ available commands:

  • module:make - Create new module
  • module:make-* - Generate components (model, controller, service, etc.)
  • module:migrate - Run module migrations
  • module:seed - Run module seeders
  • module:enable/disable - Control module status

🎯 Best Practices

  1. Keep modules focused - One module per business domain
  2. Use services for business logic - Keep controllers thin
  3. Validate with requests - Create specific request classes
  4. Format with resources - Use API resources for consistent output
  5. Test independently - Each module should have its own tests

🔄 Migration Path

When ready to scale to microservices:

  1. Each module is already self-contained
  2. Database migrations are separated
  3. API routes are modular
  4. Dependencies are clearly defined

This modular architecture provides a solid foundation for building scalable Laravel applications with clean, maintainable, and feature-focused code structure. 🚀