laravel 制作Laravel包时,如何注册依赖包的服务提供者和别名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21574413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:58:59  来源:igfitidea点击:

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

laravelpackages

提问by tprsn

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.

我正在为 Laravel 创建一个包,我已经将 Notification 包(https://github.com/edvinaskrucas/notification)定义为我的包的依赖项。

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

在 /workbench/vendor/package/src/composer.json 我有:

"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.

然后我在我的包的服务提供者的注册方法中注册服务提供者(甚至不确定这是否是正确的方法),以及使用 App::alias 的别名。

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

所以在 /workbench/vendor/package/src/Vendor/Package/PackageServiceProvider.php 我有:

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.

但是,当我尝试在控制器中使用 Notification::info() 或在视图中使用 Notification::showAll() 时,我仍然遇到“未找到类‘通知’”异常。

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

如何为我的包的依赖项正确注册服务提供者?

回答by sidneydobber

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');
}

回答by daylerees

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.

您可以在应用程序上使用 alias() 方法来注册别名,但我会考虑让您的包用户在安装过程中自己注册别名和服务提供商。这是跟踪您正在使用的外部代码的好方法,也是提取组件进行测试的好方法。

Personal opinion of course. :)

当然是个人意见。:)

Dayle

戴尔

回答by Bedram Tamang

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.

在包开发期间,您应该在 composer.json 文件中添加您的包服务提供者,如下面的代码所示。有关更多信息,请咨询 Laravel 的包发现

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