With Laravel 5.5 and the latest version of composer we now have a new thing called Package Auto-Discovery this means that installing packages in your PHP and Laravel apps will become much easier.

So, if you are a developer who is creating a package, you can add this functionality and no longer will you have to tell your users to add your ServiceProvider file to their config/app.php.

That's right, packages will now be able to auto-load or auto-discover ServiceProviders as they are being installed!

How would you implement this in your package? It's easier than you may think. Inside the composer.json file of your package you will add the following:

"extra": {
    "laravel": {
        "providers": [
            "TCG\\Voyager\\VoyagerServiceProvider"
        ]
    }
}

As you can see the above is for the Voyager package. https://github.com/the-control-group/voyager. Now, when users install the package they will no longer need to manually add the ServiceProvider.

So all together the current composer.json file in the Voyager project looks like the following:

{
    "name": "tcg/voyager",
    "description": "A Laravel Admin Package for The Control Group to make your life easier and steer your project in the right direction",
    "keywords": ["laravel", "admin", "panel"],
    "license": "MIT",
    "homepage": "https://the-control-group.github.io/voyager/",
    "support": {
        "issues": "https://github.com/the-control-group/voyager/issues",
        "source": "https://github.com/the-control-group/voyager"
    },
    "authors": [
        {
            "name": "Tony Lea",
            "email": "[email protected]"
        }
    ],
    "require": {
        "illuminate/support": "5.5.x-dev|~5.5.0",
        "intervention/image": "^2.4",
        "doctrine/dbal": "^2.5",
        "larapack/doctrine-support": "~0.1.2",
        "arrilot/laravel-widgets": "^3.7",
        "league/flysystem": "~1.0.41",
        "larapack/voyager-hooks": "~1.0.2"
    },
    "require-dev": {
        "phpunit/phpcov": "dev-master",
        "phpunit/phpunit": "~6.1",
        "laravel/framework": "5.5.x-dev",
        "orchestra/testbench": "3.5.x-dev",
        "orchestra/database": "3.5.x-dev",
        "orchestra/testbench-core": "3.5.x-dev",
        "laravel/browser-kit-testing": "2.0.x-dev",
        "orchestra/testbench-browser-kit": "3.5.x-dev",
        "codeclimate/php-test-reporter": "dev-master"
    },
    "autoload": {
        "psr-4": {
            "TCG\\Voyager\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "TCG\\Voyager\\Tests\\": "tests/"
      }
    },
    "minimum-stability": "stable",
    "extra": {
        "laravel": {
            "providers": [
                "TCG\\Voyager\\VoyagerServiceProvider"
            ]
        }
    }
}

Additionally, if you would like to add a Facade to be registered with auto-discovery you can do it like so:

"extra": {
    "laravel": {
        "providers": [
            "TCG\\Voyager\\VoyagerServiceProvider"
        ],
        "aliases": {
            "Bar": "Foo\\Bar\\Facade"
        }
    }
}

This awesome feature makes packages easier to build and easier to use :)