12

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()
{
    App::register('Krucas\Notification\NotificationServiceProvider');
    App::alias('Notification','Krucas\Notification\Facades\Notification');
}

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?

3 Answers 3

40

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.
     */
    $this->app->register('LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider');
    /*
     * Create aliases for the dependency.
     */
    $loader = \Illuminate\Foundation\AliasLoader::getInstance();
    $loader->alias('AuthorizationServer', 'LucaDegasperi\OAuth2Server\Facades\AuthorizationServerFacade');
    $loader->alias('ResourceServer', 'LucaDegasperi\OAuth2Server\Facades\ResourceServerFacade');
}
5
  • 3
    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
    Jun 24, 2016 at 6:17
  • 2
    @samrap The $this->app->alias() didn't work for me but the $loader->alias() did. That's strange
    – Ionel Lupu
    Dec 24, 2016 at 11:16
  • $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. Mar 30, 2017 at 18:42
  • $loader->alias() worked like a charm. I was wasting a lot time to solve this problem finally found the solution
    – Deemantha
    Apr 3, 2020 at 13:43
  • I'm developing a package for Laravel 8/9 and including composer dependency registering dependencies in my package provider is not working for me: ` public function register() { $this->app->register(\Tabuna\Breadcrumbs\BreadcrumbsServiceProvider::class); } //gives me: Class 'Tabuna\Breadcrumbs\BreadcrumbsServiceProvider' not found ` Mar 21, 2022 at 8:40
5

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. :)

Dayle

2
  • 1
    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?
    – tprsn
    Feb 5, 2014 at 10:41
  • 1
    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?
    – tprsn
    Feb 5, 2014 at 10:44
5

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": [
            "Barryvdh\\Debugbar\\ServiceProvider"
        ],
        "aliases": {
            "Debugbar": "Barryvdh\\Debugbar\\Facade"
        }
    }
},
3
  • 1
    Dear Bomjon, It would be nice if you add a little bit of explanation for bare code you posted.
    – Artem
    Sep 11, 2018 at 9:34
  • 1
    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 Sep 17, 2018 at 14:31
  • That didn't work for me, even thought it is written in the laravel documentation
    – shamaseen
    Sep 28, 2021 at 14:26

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.