Install the Package Run:
composer require patinthehat/laravel-require
Add the service provider to config/app.php:
LaravelRequire\LaravelRequireServiceProvider::class,
First Use Case
Install and auto-register a package (e.g., laracasts/flash):
php artisan require:package laracasts/flash
composer require and attempts to auto-detect and register the package’s Service Providers and Facades.Install + Register
php artisan require:package vendor/package
composer require and scans for:
config/app.php (via composer.json or package.xml).aliases.php (via composer.json or package.xml).Register Only
php artisan require:package vendor/package --register-only
composer require and focuses solely on auto-registering providers/aliases.Manual Fallback If auto-registration fails (e.g., non-standard package structure), manually add:
// config/app.php
Vendor\Package\ServiceProvider::class,
// config/app.php (aliases)
'Flash' => Vendor\Package\Facades\Flash::class,
Post-Install Hooks
Chain with post-install-cmd in composer.json to run custom scripts after package installation:
{
"scripts": {
"post-install-cmd": [
"php artisan require:package vendor/package"
]
}
}
CI/CD Pipelines
Use --register-only in CI to avoid redundant composer require calls if the package is already installed.
Custom Package Detection Extend the package’s logic by publishing its config (if available) and overriding:
php artisan vendor:publish --provider="LaravelRequire\LaravelRequireServiceProvider"
Composer Path Issues
composer.phar not found.composer.phar is in the project root or composer is in your PATH.php /path/to/composer.phar require vendor/package
Non-Standard Package Structure
composer.json or package.xml.Duplicate Providers
Class already exists if the package’s provider is already registered.--register-only to skip redundant checks or manually verify config/app.php.Facade Auto-Detection Limits
composer.json under "extra.aliases" or package.xml.config/app.php if needed.Verbose Output
Run with -v for detailed logs:
php artisan require:package vendor/package -v
Example output:
[OK] Installed vendor/package via Composer.
[OK] Auto-detected ServiceProvider: Vendor\Package\ServiceProvider.
[OK] Auto-detected Facade: Flash => Vendor\Package\Facades\Flash.
Dry Run Test auto-registration without installing:
php artisan require:package vendor/package --register-only --dry-run
Custom Provider/Facade Detection
Override the service provider’s detectProviders() or detectFacades() methods by extending:
class CustomLaravelRequireServiceProvider extends LaravelRequireServiceProvider {
protected function detectProviders(array $package): array {
// Custom logic here
return [...];
}
}
Register your custom provider in config/app.php.
Post-Registration Hooks
Listen for the laravel-require.registered event in your app’s EventServiceProvider:
protected $listen = [
'laravel-require.registered' => [
'App\Listeners\PostPackageRegistration',
],
];
Skip Auto-Registration Disable auto-registration globally by setting:
// config/laravel-require.php (if published)
'auto_register' => false,
Then manually run php artisan require:package --register-only when needed.
How can I help you explore Laravel packages today?