首页 > 解决方案 > Override aliases defined in package's composer.json with Laravel app config defined ones

问题描述

I am using 3 packages in my Laravel 5.8 application:

These are clashing because Backpack Base relies on the Global Alias for "Alert" being set to use PrologueAlert. See an example of how it uses \Alert here:

private function checkLicenseCodeExists()
{
    if ($this->app->environment() != 'local' && !config('backpack.base.license_code')) {
        \Alert::add('warning', "<strong>You're using unlicensed software.</strong> Please ask your web developer to <a target='_blank' href='http://backpackforlaravel.com'>purchase a license code</a> to hide this message.");
    }
}

Source: https://github.com/Laravel-Backpack/Base/blob/1.1.4/src/BaseServiceProvider.php#L264

Because I haven't bought that license yet I started seeing an error caused because that above snippet was trying to pass a string to Alert::add() but it was calling the add() method on Styde\Html\Alert\Container::add() which expects the parameter to be an instance of Styde\Html\Alert\Message instead of calling it on Prologue's version of Alert which accepts a string. It's calling the wrong "Alert"!

Even though my application is specifically set to use PrologueAlert for Alert

// config/app.php

'aliases' => [
    ...
    'Alert' => Prologue\Alerts\Facades\Alert::class
]

I have discovered the reason is that in version 1.7 Styde moved the Aliases for his package from the protected $globalAliases variable on HTMLServiceProvider.php to the composer.json auto-discover section

"extra": {
    "laravel": {
        "providers": [

        ],
        "aliases": {
            "Field": "Styde\\Html\\Facades\\Field",
            "Alert": "Styde\\Html\\Facades\\Alert",
            "Menu": "Styde\\Html\\Facades\\Menu",
            "Form": "Collective\\Html\\FormFacade",
            "Html": "Collective\\Html\\HtmlFacade"
        },
        "dont-discover": [
            "laravelcollective/html"
        ]
    }
}

Source: https://github.com/StydeNet/html/commit/f51138fb42bef458f3f0e101b98344162b7327ba#diff-b5d0ee8c97c7abd7e3fa29b9a27d1780

Now, it seems my application is prioritising Styde's alias of "Alert" over my own application-set value!

Other than rolling back to use version 1.6 of Styde, how can I force Laravel to prioritise my own defined aliases over ones discovered via the composer.json?

标签: laravellaravel-backpack

解决方案


我找到了解决方案!它实际上是受到我原始帖子中的一个片段的启发。

你可以在你的应用程序中添加一个extra部分,composer.json由 Laravel 应用程序读取并用于决定在自动发现期间忽略哪些包,如下所示:

// composer.json
{
    ...
    "extra": {
        "laravel": {
            "dont-discover": [
                "styde/html"
            ]
        }
    }
}

然后,这允许您从有问题的包中挑选和选择别名,并在您的应用程序中定义尽可能多或尽可能少的别名config/app.php (对于我的应用程序,我只使用Field来自 Styde/Html 的别名,所以这是我唯一的一个添加到我的应用程序配置)

我认为随着越来越多的包维护者开始利用自动发现功能,这将成为一个更广泛使用的功能。

事后思考:这是 Composer 和 Laravel 之间关系的转变。传统上,composer.json 文件只是一个包管理器,它会在安装时运行,然后在应用程序运行时不使用,而现在它是一个由应用程序读取的配置文件。当我们的管道打包和部署用于整理生产环境中不需要的文件的代码时,我很难学到这一点。它正在删除 composer.json,这导致错误再次在我们的 QA 环境中发生。


推荐阅读