Auto Discovery was introduced in Laravel 5.5. Auto-discovery feature allows to automatically register package service providers and services which eliminates manual configuration needs.
It helps to speed up the package installation process and reduces any possible human errors. But sometimes, there are situations where we need to have more control over what needs to be loaded and when.
In such cases, we can use the Disabling Auto Discovery feature of laravel. By Disabling Auto Discovery in Laravel, we can explicitly control which service providers are loaded for your application and also decide in what order they need to be loaded.
Before Auto Discovery, we had to manually register the service providers and facades in the config/app.php file. However, with Auto Discovery, Laravel automatically detects and registers them.
// Before Auto Discovery, you had to manually add service providers and facades 'providers' => [ // Other Service Providers... // Manually added Service Provider Package\ServiceProvider::class, ], 'aliases' => [ // Other Facades... // Manually added Facade 'PackageFacade' => Package\Facade::class, ],
Lets learn step-by-step, how to disable auto discovery in laravel:
Open your composer.json file located in the root directory of your Laravel application.
In the composer.json file, you can add the dont-discover option in the extra section. This option allows you to specify the packages for which you want to disable Auto Discovery.
"extra": { "laravel": { "dont-discover": [ "Vendor/PackageName" ] } }
In the above code, replace “Vendor/PackageName” with the name of the package for which you want to disable Auto Discovery.
If you want to disable Auto-Discovery for all packages, you can use the * wildcard.
"extra": { "laravel": { "dont-discover": [ "*" ] } }
After making changes to your composer.json file, you need to update your dependencies. You can do this by running the composer update command in your terminal.
composer update
Disabling auto-discovery of packages often raises issues over providing control to the application. Here are the few issues related to disabling auto-discovery of packages in laravel:
This is the most common error related to disabling auto discovery. This error occurs when Laravel tries to use a package that hasn’t been manually registered.
Solution: Ensure that you have manually registered all the necessary service providers and facades in config/app.php file for the packages listed in the dont-discover array.
Another issue could be the incorrect order of service providers. The order in which service providers are loaded can affect how your application works.
Solution: Ensure that you have arranged the service providers in the correct order in the app.php file.
Disabling Auto Discovery might also lead to performance issues if not done correctly. This is because Laravel would not be able to optimize the loading of service providers and facades.
Solution: If you are facing performance issues, consider enabling Auto Discovery for some of the packages.