ixspx / module-generator
Generate module structure in Laravel
Requires
- php: ^8.1 || ^8.3
- illuminate/console: ^10.0 || ^11.0 || ^12.0
- illuminate/filesystem: ^10.0 || ^11.0 || ^12.0
- illuminate/support: ^10.0 || ^11.0 || ^12.0
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
README
Module-Generator in Laravel
Laravel Module Generator with layered architecture: Model, Repository, Service, Controller, Provider. Generate a complete module structure in Laravel, including:
- Model
- Repositories/Repository + Interface
- Service
- Controller
- Service Provider
π Architecture Overview
Generate Module
Generated modules follow a layered serviceβrepository architecture:
ModuleName/
βββ Models/
β βββ ModuleName.php
β
βββ Repositories/
β βββ Interfaces/
β β βββ ModuleName/
β β βββ ModuleNameRepositoryInterface.php
β βββ Repository/
β βββ ModuleName/
β βββ ModuleNameRepository.php
β
βββ Services/
β βββ ModuleName/
β βββ ModuleNameService.php
β
βββ Http/
β βββ Controllers/
β βββ ModuleName/
β βββ ModuleNameController.php
β
βββ Providers/
βββ ModuleNameServiceProvider.php
Generate Standard API
This package provides a standardized API foundation for Laravel applications by implementing a Centralized API Response & Exception Handling Pattern. All HTTP responses are forced into a unified JSON format, and all exceptions are handled in a single, centralized layer, ensuring consistency across the entire application. High-level flow
Exceptions β Middleware / Exception Handler β Global API Response
β¨ Key Features
- β Unified JSON response structure
- β Centralized exception handling
- β Opinionated API contract (consistent success & error responses)
- β Middleware-based response enforcement
- β Framework-agnostic business logic (HTTP-agnostic services)
- β Suitable for packages, microservices, and large-scale APIs
π§ Architectural Principles
This package follows Clean Architectureβinspired layering, where each layer has a single and well-defined responsibility.
-
Model Represents the database table (Eloquent ORM).
- β No business logic
- β No complex queries
-
Repository + Interface Encapsulates all data access logic and abstracts the persistence layer.
- Defines contracts via interfaces
- Implements database queries (Eloquent, Query Builder, etc.)
Benefits:
- β Enables easy testing (mocking repositories)
- β Allows swapping data sources without affecting business logic 3. Service
-
Service Contains business rules and application use cases. Responsibilities:
- Orchestrates workflows
- Applies domain validation
Throws domain-specific exceptions
- β HTTP-agnostic
- β No request / response handling
- β No direct database queries
-
Controller Acts as the delivery layer. Responsibilities:
- Receives HTTP requests.
- Delegates execution to services.
- Returns standardized API responses.
Controllers remain thin and predictable.
-
Service Provider Responsible for dependency injection configuration.
- Binds interfaces to concrete implementations.
- Registers package services, middleware, and handlers.
This ensures loose coupling and extensibility.
Centralized Exception Handling
All exceptionsβframework, validation, authorization, or domain-specificβare handled in a single place and transformed into a standardized API response. This pattern is also known as:
- Exception-to-Response Mapping.
- API Response Envelope Pattern.
- Opinionated API Layer.
Requirements
- PHP >= 8.1
- Laravel 9, 10, 11, 12
Installation
Install via Composer:
composer require ixspx/module-generator
β Manual Provider Registration
If package discovery is disabled, register the provider manually in bootstrap/providers.php:
'providers' => [
// Other service providers...
Ixspx\ModuleGenerator\Providers\ModuleGeneratorServiceProvider::class,
]
π Usage
Generate Standard API Structure
Run the following command to generate the standard API structure:
php artisan make:api-install
You may add the --force option to overwrite existing API files.
Register API Configuration
After running make:api-install, you must manually register the API configuration in bootstrap/app.php:
use Illuminate\Support\Facades\Route;
use App\Exceptions\ApiExceptionRegistrar;
use App\Http\Middleware\ForceJsonResponse;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__ . '/../routes/web.php',
commands: __DIR__ . '/../routes/console.php',
health: '/up',
then: function ($router) {
Route::prefix('api/v1')
->group(base_path('routes/api.php'));
}
)
->withMiddleware(function (Middleware $middleware): void {
$middleware->append(ForceJsonResponse::class);
})
->withExceptions(function (Exceptions $exceptions): void {
ApiExceptionRegistrar::register($exceptions);
})->create();
Generate a Module
Generate a new module using the following command:
php artisan make:mod {{nameModule}}
Example:
php artisan make:mod OrderPayment
After the command finished, you will see a notification simillar to the flowing:
β οΈ Important:
You must register the generated module service provider manually in bootstrap/providers.php. This command generates a complete module structure based on a predefined layered architecture (Controller, Service, Repository, Interface, etc.).
OR Generate API Response Helper
To generate the API response helper, run:
php artisan make:api-response
π License
This project is open-source software licensed under the MIT License. See the see LICENSE
