PHP code example of uncover / modular-monolith-laravel

1. Go to this page and download the library: Download uncover/modular-monolith-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

uncover / modular-monolith-laravel example snippets


use App\Http\Controllers\EmailVerificationController;

// Basic usage
Route::moduleResource('email-verifications', EmailVerificationController::class);

// With middleware
Route::moduleResource('email-verifications', EmailVerificationController::class, [
    'middleware' => ['auth:sanctum', 'EmailVerificationMiddleware']
]);

// config/modular.php

// Customize paths
'paths' => [
    'controller' => app_path('Http/Controllers'),
    'model' => app_path('Models'),
    'validation' => app_path('Validations'),
    'middleware' => app_path('Http/Middleware'),
    'service' => app_path('Services'),
    'observer' => app_path('Observers'),
    'migration' => database_path('migrations'),
],

// Customize namespaces
'namespaces' => [
    'controller' => 'App\Http\Controllers',
    'model' => 'App\Models',
    'validation' => 'App\Validations',
    'middleware' => 'App\Http\Middleware',
    'service' => 'App\Services',
    'observer' => 'App\Observers',
],

// Route configuration
'routes' => [
    'middleware' => ['api'],
    'use_plural' => true,      // email-verifications vs email-verification
    'separator' => '-',        // email-verifications vs emailVerifications
],

class EmailVerificationController extends ModularController
{
    protected static string $model = EmailVerification::class;
    protected static string $validation = EmailVerificationValidation::class;
    
    // Add custom methods if needed
    public function customMethod(Request $request)
    {
        // Your custom logic
    }
}

class EmailVerification extends ModularModel
{
    protected $table = 'email_verifications';
    
    protected $fillable = [
        'uuid',
        // Add your fillable attributes here
    ];
}

class EmailVerificationValidation extends ModularValidation
{
    protected static function createRules(Request $request): array
    {
        return [
            'email' => ['}

class EmailVerificationMiddleware extends ModularMiddleware
{
    protected array $actionMethodMap = [
        'create' => 'authorizeCreate',
        'read' => 'authorizeRead',
        'update' => 'authorizeUpdate',
        'delete' => 'authorizeDelete',
        'query' => 'authorizeQuery',
    ];
    
    protected function authorizeCreate(Request $request): bool
    {
        return auth()->user()->can('email-verification.create');
    }
}

class EmailVerificationService extends ModularService
{
    protected static string $model = EmailVerification::class;
    protected static ?int $cacheDuration = 3600; // Enable caching
    
    // Add custom methods
    public static function findByEmail(string $email): ?EmailVerification
    {
        return static::getModel()::where('email', $email)->first();
    }
}

class EmailVerificationObserver extends ModularObserver
{
    public static function getObservedModel(): string
    {
        return EmailVerification::class;
    }
    
    public function created(EmailVerification $model): void
    {
        // Handle after creation
    }
}

// AppServiceProvider.php
public function boot()
{
    EmailVerificationObserver::register();
}
bash
php artisan vendor:publish --tag=modular-config
bash
php artisan vendor:publish --tag=modular-base-classes
bash
php artisan vendor:publish --tag=modular-stubs