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

前端 未结 3 1958
死守一世寂寞
死守一世寂寞 2021-01-31 12:26

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 /workbe

相关标签:
3条回答
  • 2021-01-31 12:32

    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

    0 讨论(0)
  • 2021-01-31 12:42

    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');
    }
    
    0 讨论(0)
  • 2021-01-31 12:45

    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"
            }
        }
    },
    
    0 讨论(0)
提交回复
热议问题