Home » Laravel Disable Auto Discovery – 6 Simplified Ways To Do That

Laravel Disable Auto Discovery – 6 Simplified Ways To Do That

Loved it? Please Share on Social Media

INTRODUCTION:

Hello, fellow developers. What’s up?, I hope you are doing fine. In this extended tutorial, we will learn how to disable auto discovery in laravel. We will discuss six simplified ways to disable auto discovery so you can follow it easily. Well, let’s start our tutorial.  

What is Auto Discovery In Laravel?:

Basically, auto discovery is a feature that was first introduced to Laravel 5.5. It works while we are installing a laravel dependency package. Auto discovery will publish and register all of the ServiceProviders & Facades files of that package automatically into our laravel application.

How the Auto Discovery Works In Laravel?:

In this tutorial, we will use the laravel debugbar package from barryvdh for better learning. First, we will go to vendor>barryvdh>laravel-debugbar directory. Then we will open that composer.json file. Now, take a look at the extra key. You will see providers and aliases keys. Inside those keys, you see those package entries. Which will be discovered when we will discover the package. For better understanding please follow the below code snippet for just learning purposes.

"extra": {
        "branch-alias": {
            "dev-master": "3.10-dev"
        },

        "laravel": {
            "providers": [
                "Barryvdh\\Debugbar\\ServiceProvider"
            ],
            "aliases": {
                "Debugbar": "Barryvdh\\Debugbar\\Facades\\Debugbar"
            }
        }
    },

Laravel Disable Auto Discovery on .ENV File:

This is the simplest way to laravel disable auto discovery. For that, you just need to use an environment variable which is DISABLE_DISCOVERY=false. If you want to enable it must use true value instead of false.

Disable Auto Discovery by Updating Composer.json File:

This is the most common & popular way to disable your auto discovery. For that, you must add the extra key inside your composer.json file. Just copy & paste the below code at last into your composer.json file.

"extra": {
    "laravel": {
        "dont-discover": [
            "barryvdh/laravel-debugger"
        ]
    }
}

You just need to insert your desired package directory like the upper example. You can also deactivate auto discovery for your all packages using the * character. Please follow the below code snippet for more understanding.

Note: It is not a recommended task. It may cause issues with your application.

"extra": {
    "laravel": {
        "dont-discover": [
            "*"
        ]
    }
}

Disable Auto Discovery by Updating App.php File:

You can deactivate auto discovery by making changes on config>app.php file. You just need to remove your package entries from both the ‘providers’ and ‘aliases’ array. After that save the file and you php artisan cache:clear php artisan config:clear command.

Disable Auto Discovery Creating Your ServiceProvider File:

You can deactivate auto discovery by creating your service provider to deactivate this. In this case, I already created a DisableAutoDiscoveryProvider.php file you just need to copy & paste the following code into your file.

namespace App\Providers;
use Illuminate\Support\ServiceProvider;

class DisableAutoDiscoveryProvider extends ServiceProvider

{
    public function register()
    {
        if (env('DISABLE_DISCOVERY')) {
            $this->app->getLoadedProviders()['Illuminate\Foundation\Providers\DiscoverServiceProvider']->defer();
        }
    }
}

Disable Auto Discovery Creating Your Middleware File:

You can deactivate auto discovery by creating your middleware file to deactivate this. In this case, I already created a DisableAutoDiscoveryMiddleware.php file you just need to copy & paste the following code into your file.

namespace App\Http\Middleware;
use Closure;

class DisableAutoDiscoveryMiddleware

{
    public function handle($request, Closure $next)
    {
        if (env('DISABLE_DISCOVERY')) {
            $this->app->getLoadedProviders()['Illuminate\Foundation\Providers\DiscoverServiceProvider']->defer();
        }
        return $next($request);
    }
}

Disable Auto Discovery by Making it Automatic:

Sometimes, your application needs to swap the auto discovery by enabling and disabling it. It might help you to prevent sudden errors of packages that are related to disabling auto discovery. 

To do this thing automatically, you need to create an AutomaticDiscoveryServiceProvider.php file. you just need to copy & paste the following code into your file.

namespace App\Providers;
use Illuminate\Support\ServiceProvider;

class AutomaticDiscoveryServiceProvider.php extends ServiceProvider

{
    public function register()
    {
        if (rand(0, 1)) {
            // Enable auto-discovery
            $this->app->getLoadedProviders()['Illuminate\Foundation\Providers\DiscoverServiceProvider']->defer(false);
        } else {
            // Disable auto-discovery
            $this->app->getLoadedProviders()['Illuminate\Foundation\Providers\DiscoverServiceProvider']->defer(true);
        }
    }
}

After that, you have to register this AutomaticDiscoveryServiceProvider into your config>app.php file. copy & paste the following line into the aliases key like below.

'providers' => [
    // Other providers...
    App\Providers\AutomaticDiscoveryServiceProvider::class,
],

At last, you have to run php artisan config:clearand php artisan cache:clear commands.

Tips for Avoiding Unexpected Errors/ISSUES:

Whenever you working with laravel disable auto discovery tasks, and always keep in mind that it may create some errors/issues due to internal code conflict/different composer versions/different laravel framework versions, etc. To prevent that kind of problem you always have to test your application by enabling/disabling the package. You can check out the following code snippet for better learning.

"extra": {
    "laravel": {
        "dont-discover": {
            "barryvdh/laravel-debugger": true,
            "spatie/laravel-permission": false
        }
    }
}

As you can see, I’ve disabled the spatie/laravel-permission because it’s causing some issues. You can do the same thing If you want to prevent this kind of problem easily and also it will help your application up and running.

Quick Note:

I’ve explained six ways to disable laravel auto discovery in this tutorial. Sometimes, some tips will work, and some will not work. You must check every way I’ve told you to follow and practice. You can learn more about this topic from their documentation.

CONCLUSION:

Finished! That’s all about our tutorial. I’ve tried to teach you the most updated & tested things in this tutorial. Thank you so much for reading the whole tutorial from the beginning. If this tutorial helps you a little bit, then remember to share this post on social media. If you have questions, suggestions, or tips regarding this post, let us know via our Contact Us page.


Loved it? Please Share on Social Media

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.