When making a Laravel package, how do I register the service provider and alias of dependency packages?

laravel package development
laravel service provider example
route service provider laravel
how to install package in laravel
laravel package generator
appserviceprovider laravel
php artisan package:discover

I'm creating a package for Laravel and I've defined the Notification package (https://github.com/edvinaskrucas/notification) as a dependency for my package.

In /workbench/vendor/package/src/composer.json I have:

"require": {
    "php": ">=5.3.0",
    "illuminate/support": "4.1.*",
    "edvinaskrucas/notification": "2.*"

I'm then registering the service provider in my package's service provider's register method (not even sure if this is the right way to do this), and the alias using App::alias.

So in /workbench/vendor/package/src/Vendor/Package/PackageServiceProvider.php I have:

public function register()

But I'm still getting "Class 'Notification' not found" exception when attempting to use Notification::info() in a controller or Notification::showAll() in a view.

How do I properly register service providers for my package's dependencies?

I had the same problem. I had a dependency in a package and didn't want to bother the user with these dependencies, for it was a dependency in a dependency. So this is the solution. Hope it will help you!

public function register()
     * Register the service provider for the dependency.
     * Create aliases for the dependency.
    $loader = \Illuminate\Foundation\AliasLoader::getInstance();
    $loader->alias('AuthorizationServer', 'LucaDegasperi\OAuth2Server\Facades\AuthorizationServerFacade');
    $loader->alias('ResourceServer', 'LucaDegasperi\OAuth2Server\Facades\ResourceServerFacade');

Package Development - Laravel, I had a dependency in a package and didn't want to bother the user with these You can use the alias() method on the app to register an alias, but I would During package development you should add your package service provider in in the example, Please check in laravel.com/docs/5.6/packages#package-​discovery  Once you create a service provider, you also need to register it with the Laravel application in order to actually use it, so we'll go through that as well. There are two important methods, boot and register, that your service provider may implement, and in the last segment of this article we'll discuss these two methods thoroughly.

You can use the alias() method on the app to register an alias, but I would consider having your package users register aliases and service providers themselves in the install process. It's a good way of keeping track of the external code that you're using, and a nice way of pulling components out to test things.

Personal opinion of course. :)


Service Providers - Laravel, package to the Laravel app - you shouldn't do this manually, as it would make using your package unecessarily complicated. Fortunately, Laravel makes it easy for you to register packages from within your custom package's service provider class. You can also load up an alias for the package:. The best way to develop a package to pull it into a Laravel application like it would be an existing package. We can do it via composer (that’s why it was important to set up the package’s composer first). So what we can do to define repositories in our application’s composer.json and pull the pre-defined local package as a dependency.

During package development you should add your package service provider in composer.json file as in the code below. For additional information please consult at Laravel's Package Discovery.

"extra": {
    "laravel": {
        "providers": [
        "aliases": {
            "Debugbar": "Barryvdh\\Debugbar\\Facade"

Managing Dependencies in a Laravel Package, I started by making a new folder and running composer init to generate a As per most PHP packages, I created a src directory where all the php files will live. to allow Laravel to automatically register your service provider when the in the aliases array, but since I don't have any in this package, I didn't. Developing Composer packages locally through a local file symlink speeds up development immensely when you want to create Laravel packages and try them out on a real application. I was reading about a fancy bash alias by Caleb Porzio, which is a bash alias inspired by npm link.

Creating your first Laravel package, I was reading about a fancy bash alias by Caleb Porzio, which is a bash alias inspired by npm link . I am going to walk you through creating a quick Composer package from First, let's create an example composer project and hook up a Laravel service provider. public function register() { # code } }  Most service providers contain a register and a boot method. Within the register method, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method. The Artisan CLI can generate a new provider via the make:provider command:

Developing Laravel Packages with Local Composer Dependencies , Composer is a tool for dependency management in PHP. Some Laravel packages require a few extra steps for it to be usable in a Laravel project: Register service providers; Register Aliases/Facades; Publish assets $​providers = Collection::make($this->config['app.providers']) ->partition(function  If you do not run Laravel 5.5 (or higher), then add the service provider in config/app.php: JeroenG\Packager\ PackagerServiceProvider :: class , If you do run the package on Laravel 5.5+, package auto-discovery takes care of the magic of adding the service provider.

Package Auto-discovery in Laravel, and I teamed up to make it much easier to install and enable packages… And the package's service provider will automatically be registered and enabled: its service provider will only be registered when your “dev” dependencies are packages and their discovered providers and aliases will be removed as well:. A Service Provider informs Laravel about any dependency we need to bind or resolve to the service container. You can read the this post, Service Providers in Laravel to learn more about service providers.

  • Just fyi, there is no need to get an instance of the AliasLoader class, as the container has a method for adding aliases. $this->app->alias() will work the same way :) github.com/laravel/framework/blob/5.2/src/Illuminate/Container/…
  • @samrap The $this->app->alias() didn't work for me but the $loader->alias() did. That's strange
  • $this->app->alias() method is misleading it does not take a typical facade alias definition as an argument, but rather the result of the facades getFacadeAccessor(). Or so it seems to me.
  • Okay, thanks. Hmmm, I see what you're saying. I've never seen other packages do that (i.e. I've only had to add one service provider and one alias to app/config/app.php when installing a package, even if that package has other dependencies). Are these developers likely using App::register and App::alias, rather than your preference of having the user add them in their config?
  • I'm still getting Notification class not found exception after adding App::alias (assuming I'm using the right syntax, googled around but can't find any documentation on App::alias?). I've updated my original question. What am I doing wrong? Is it because my controller is in the package namespace?
  • Dear Bomjon, It would be nice if you add a little bit of explanation for bare code you posted.
  • During package development, You should add your package service provider in composer.json file like above in the example, Please check in laravel.com/docs/5.6/packages#package-discovery