Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel App Laravel Package

binetvn/laravel-app

Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require binetvn/laravel-app
    

    Publish the package configuration (if needed):

    php artisan vendor:publish --provider="Binetvn\LaravelApp\LaravelAppServiceProvider" --tag="config"
    
  2. First Use Case: Basic Module Structure Create a new module (e.g., UserManagement) using the package’s scaffolding:

    php artisan app:module UserManagement
    

    This generates:

    • app/Modules/UserManagement/
      • Http/Controllers/ (API/HTTP controllers)
      • Repositories/ (Eloquent/Repository interfaces)
      • Services/ (Business logic)
      • Resources/ (Views, JSON responses)
      • Routes/ (Module-specific routes)
      • Database/Migrations/ (Module migrations)
  3. Register the Module Add the module to config/app.php under modules:

    'modules' => [
        'Binetvn\LaravelApp\Modules\UserManagement\UserManagementModule',
    ],
    

    Run migrations:

    php artisan migrate
    
  4. First Route Define a route in app/Modules/UserManagement/Routes/web.php:

    Route::get('/users', 'UserController@index');
    

    The package auto-loads module routes when the module is registered.


Implementation Patterns

1. Modular Routing

  • Pattern: Group routes by module.
    // app/Modules/UserManagement/Routes/web.php
    Route::prefix('admin')->group(function () {
        Route::resource('users', 'UserController');
    });
    
  • Integration Tip: Use middleware per module:
    Route::middleware(['auth', 'admin'])->group(function () {
        // Module routes
    });
    

2. Repository Pattern

  • Pattern: Decouple controllers from Eloquent.
    // app/Modules/UserManagement/Repositories/UserRepository.php
    public function findAll()
    {
        return User::all();
    }
    
  • Usage in Controller:
    public function index(UserRepository $repository)
    {
        return $repository->findAll();
    }
    

3. Service Layer

  • Pattern: Move business logic to services.
    // app/Modules/UserManagement/Services/UserService.php
    public function createUser(array $data)
    {
        $user = User::create($data);
        // Add custom logic (e.g., send welcome email)
        return $user;
    }
    
  • Dependency Injection:
    public function store(Request $request, UserService $service)
    {
        return $service->createUser($request->all());
    }
    

4. API Responses

  • Pattern: Standardize JSON responses.
    // app/Modules/UserManagement/Resources/ApiResponse.php
    public static function success($data, $message = null)
    {
        return response()->json([
            'success' => true,
            'data' => $data,
            'message' => $message,
        ]);
    }
    
  • Usage:
    return ApiResponse::success($user);
    

5. Database Migrations

  • Pattern: Scope migrations to modules.
    php artisan make:migration create_users_table --module=UserManagement
    
    Migrations are auto-loaded from app/Modules/{Module}/Database/Migrations/.

6. Views and Assets

  • Pattern: Isolate module assets.
    // app/Modules/UserManagement/Resources/views/users/index.blade.php
    
  • Usage in Controller:
    return view('UserManagement::users.index');
    
  • Assets: Publish module-specific assets via:
    php artisan app:publish-assets UserManagement
    

7. Testing

  • Pattern: Test modules in isolation.
    // tests/Module/UserManagement/UserTest.php
    public function test_user_creation()
    {
        $response = $this->post('/api/users', ['name' => 'Test']);
        $response->assertStatus(201);
    }
    
  • Integration Tip: Use ModuleTestCase (if provided by the package) to mock dependencies.

Gotchas and Tips

Pitfalls

  1. Route Caching Conflicts

    • If routes aren’t loading, clear the route cache:
      php artisan route:clear
      
    • Ensure modules are registered in config/app.php before routes are cached.
  2. Service Provider Order

    • Modules must be registered after Laravel’s core providers. Check config/app.php order.
  3. Migration Paths

    • Migrations must be in app/Modules/{Module}/Database/Migrations/ or they won’t auto-load.
    • Run php artisan migrate --path=app/Modules/{Module}/Database/Migrations for targeted migrations.
  4. Namespace Collisions

    • Avoid naming modules with reserved Laravel words (e.g., Auth, Cache). Use prefixes like UserManagement.
  5. Dependency Injection Issues

    • If services/repositories aren’t injected, ensure:
      • They’re in the App\Modules\{Module}\ namespace.
      • Their interfaces (if used) are properly bound in the module’s service provider.

Debugging Tips

  1. Check Module Registration Run php artisan app:list-modules to verify registered modules.

  2. Route Debugging Use php artisan route:list to see if module routes appear. Filter by module prefix.

  3. Service Binding Dump the service container to check bindings:

    dd(app()->bindings());
    
  4. Configuration Overrides Publish and override config:

    php artisan vendor:publish --tag="app-config"
    

    Modify config/app.php under the modules section.

Extension Points

  1. Custom Module Templates Override scaffolding templates by publishing and modifying:

    php artisan vendor:publish --tag="app-module-templates"
    
  2. Dynamic Module Loading Load modules conditionally in AppServiceProvider:

    if (config('app.enable_user_module')) {
        $this->app->register(\App\Modules\UserManagement\UserManagementModule::class);
    }
    
  3. Event Handling Dispatch module-specific events:

    // In a service
    event(new \App\Modules\UserManagement\Events\UserCreated($user));
    

    Listen in EventServiceProvider:

    protected $listen = [
        'App\Modules\UserManagement\Events\UserCreated' => [
            'App\Modules\UserManagement\Listeners\SendWelcomeEmail',
        ],
    ];
    
  4. API Versioning Use route prefixes for versioning:

    Route::prefix('v1')->group(function () {
        // Module routes
    });
    
  5. Localization Isolate translations per module:

    php artisan lang:publish --module=UserManagement
    

    Load translations in views:

    @lang('UserManagement::messages.welcome')
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours