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"
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).
Auto-Discovery:
config/autoreg.php) for Laravel components (providers, routes, migrations, etc.).routes/custom/ instead of routes/web.php. Update the config to include:
'routes' => [
base_path('routes/custom/*.php'),
],
Integration with Existing Code:
// app/Providers/NonStandard/CustomAuthProvider.php
namespace App\Providers\NonStandard;
use Illuminate\Support\ServiceProvider;
class CustomAuthProvider extends ServiceProvider { ... }
app/Console/Commands/NonStandard/ and register them via:
'commands' => [
app_path('Console/Commands/NonStandard/*.php'),
],
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'),
],
Blade and Translations:
resources/views/custom/:
'views' => [
resource_path('views/custom/*.blade.php'),
],
resources/lang/custom/:
'lang' => [
resource_path('lang/custom/*.php'),
],
Migrations and Broadcast Channels:
database/migrations/nonstandard/:
'migrations' => [
database_path('migrations/nonstandard/*.php'),
],
app/Broadcasting/NonStandard/:
'channels' => [
app_path('Broadcasting/NonStandard/*.php'),
],
Caching Issues:
config/autoreg.php, clear Laravel’s caches:
php artisan config:clear
php artisan cache:clear
php artisan view:clear
Naming Conventions:
*ServiceProvider.php). Rename files to match conventions or explicitly register them in the config:
'providers' => [
app_path('Providers/NonStandard/MyProvider.php'),
],
Overriding Defaults:
app/Providers/), exclude the default paths:
'providers' => [
app_path('Providers/NonStandard/*.php'),
],
// Explicitly exclude the default directory to avoid duplication.
Debugging:
config/autoreg.php:
'debug' => true,
autoreg log channel for discovery issues.Performance:
*.php) to limit scans.Dynamic Paths: Use helper methods in the config to generate paths dynamically:
'routes' => [
base_path('routes/'.config('app.env').'/*.php'),
],
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'
);
Testing: Mock the package’s discovery logic in tests:
$this->app->shouldDiscoverProviders = false; // Disable auto-discovery
$this->app->register(\App\Providers\CustomServiceProvider::class);
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();
});
Excluding Files: Use negated patterns to exclude specific files (e.g., test providers):
'providers' => [
app_path('Providers/NonStandard/*.php'),
'!app/Providers/NonStandard/Test*.php',
],
How can I help you explore Laravel packages today?