制作Laravel软件包时,如何注册服务提供者和依赖软件包的别名? [英] When making a Laravel package, how do I register the service provider and alias of dependency packages?

查看:58
本文介绍了制作Laravel软件包时,如何注册服务提供者和依赖软件包的别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Laravel创建一个程序包,并定义了Notification程序包( https://github.com/edvinaskrucas/notification )作为我程序包的依赖项.

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.

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

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

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

然后我要使用包的服务提供者的register方法(甚至不确定这样做是否正确)和服务别名使用App :: alias来注册服务提供者.

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.

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

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

但是,当尝试在控制器中使用Notification :: info()或在视图中使用Notification :: showAll()时,仍然出现找不到类'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?

推荐答案

我遇到了同样的问题.我在程序包中有一个依赖项,不想用这些依赖项来打扰用户,因为它是依赖项中的依赖项.所以这就是解决方案.希望对您有帮助!

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

这篇关于制作Laravel软件包时,如何注册服务提供者和依赖软件包的别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆