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 Auto Reg Laravel Package

code-distortion/laravel-auto-reg

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require code-distortion/laravel-auto-reg
    

    Publish the config (optional but recommended for customization):

    php artisan vendor:publish --provider="CodeDistortion\AutoReg\AutoRegServiceProvider" --tag="config"
    
  2. First Use Case: Place your custom files in non-standard directories (e.g., app/Providers/NonStandard/ instead of app/Providers/). The package will auto-discover and register them during bootstrapping.

    Example structure:

    /app
      /Providers/NonStandard
        - CustomServiceProvider.php
    

    No additional code is needed—just ensure your files follow Laravel’s naming conventions (e.g., *ServiceProvider.php for providers).


Implementation Patterns

Core Workflows

  1. Auto-Discovery:

    • The package scans predefined directories (configurable in config/autoreg.php) for Laravel components (providers, routes, migrations, etc.).
    • Example: Place routes in routes/custom/ instead of routes/web.php. Update the config to include:
      'routes' => [
          base_path('routes/custom/*.php'),
      ],
      
  2. Integration with Existing Code:

    • Service Providers: Extend or override default providers by placing them in custom directories. The package merges registrations.
      // app/Providers/NonStandard/CustomAuthProvider.php
      namespace App\Providers\NonStandard;
      use Illuminate\Support\ServiceProvider;
      class CustomAuthProvider extends ServiceProvider { ... }
      
    • Commands: Store commands in app/Console/Commands/NonStandard/ and register them via:
      'commands' => [
          app_path('Console/Commands/NonStandard/*.php'),
      ],
      
  3. Conditional Registration: Use environment-specific directories (e.g., app/Providers/Staging/) and configure the package to load them conditionally:

    'providers' => [
        app_path('Providers/'.env('APP_ENV').'/*.php'),
    ],
    
  4. Blade and Translations:

    • Auto-load Blade templates from resources/views/custom/:
      'views' => [
          resource_path('views/custom/*.blade.php'),
      ],
      
    • Auto-load translations from resources/lang/custom/:
      'lang' => [
          resource_path('lang/custom/*.php'),
      ],
      
  5. Migrations and Broadcast Channels:

    • Register migrations in database/migrations/nonstandard/:
      'migrations' => [
          database_path('migrations/nonstandard/*.php'),
      ],
      
    • Register broadcast channels in app/Broadcasting/NonStandard/:
      'channels' => [
          app_path('Broadcasting/NonStandard/*.php'),
      ],
      

Gotchas and Tips

Common Pitfalls

  1. Caching Issues:

    • After updating config/autoreg.php, clear Laravel’s caches:
      php artisan config:clear
      php artisan cache:clear
      php artisan view:clear
      
    • If routes or providers aren’t loading, verify the paths in the config are correct and files are readable.
  2. Naming Conventions:

    • The package relies on Laravel’s auto-discovery rules (e.g., *ServiceProvider.php). Rename files to match conventions or explicitly register them in the config:
      'providers' => [
          app_path('Providers/NonStandard/MyProvider.php'),
      ],
      
  3. Overriding Defaults:

    • If the package conflicts with default Laravel discovery (e.g., app/Providers/), exclude the default paths:
      'providers' => [
          app_path('Providers/NonStandard/*.php'),
      ],
      // Explicitly exclude the default directory to avoid duplication.
      
  4. Debugging:

    • Enable verbose logging in config/autoreg.php:
      'debug' => true,
      
    • Check the autoreg log channel for discovery issues.
  5. Performance:

    • Auto-discovery adds minimal overhead, but avoid scanning thousands of files in a single directory. Use specific glob patterns (e.g., *.php) to limit scans.

Pro Tips

  1. Dynamic Paths: Use helper methods in the config to generate paths dynamically:

    'routes' => [
        base_path('routes/'.config('app.env').'/*.php'),
    ],
    
  2. Environment-Specific Configs: Override config/autoreg.php per environment (e.g., config/autoreg.staging.php) and load it conditionally in AppServiceProvider:

    $this->mergeConfigFrom(
        config_path('autoreg.'.$this->app->environment.'.php'),
        'autoreg'
    );
    
  3. Testing: Mock the package’s discovery logic in tests:

    $this->app->shouldDiscoverProviders = false; // Disable auto-discovery
    $this->app->register(\App\Providers\CustomServiceProvider::class);
    
  4. Extending Discovery: Create a custom discovery class by extending CodeDistortion\AutoReg\Discovery\Discovery and bind it in AppServiceProvider:

    $this->app->bind(\CodeDistortion\AutoReg\Discovery\Discovery::class, function () {
        return new \App\Services\CustomDiscovery();
    });
    
  5. Excluding Files: Use negated patterns to exclude specific files (e.g., test providers):

    'providers' => [
        app_path('Providers/NonStandard/*.php'),
        '!app/Providers/NonStandard/Test*.php',
    ],
    
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.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle