laravel - 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:
- Backpack CRUD (which uses Backpack Base) https://github.com/Laravel-Backpack/CRUD
- Styde\HTML https://github.com/StydeNet/html/
- Prologue\Alerts https://github.com/prologuephp/alerts
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"
]
}
}
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?
解决方案
我找到了解决方案!它实际上是受到我原始帖子中的一个片段的启发。
你可以在你的应用程序中添加一个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 环境中发生。
推荐阅读
- android - Flutter + android在Android中的Firebase Cloud Messaging上接收数据消息
- excel - 在一行的单元格范围内设置背景颜色(不使用条件格式)
- xamarin.forms - 在 Xamarin 表单中重用导航栏字体
- javascript - AWS Amplify 错误:(节点:68674)UnhandledPromiseRejectionWarning:NoSuchKey:指定的密钥不存在
- bash - Kubernetes postStart 生命周期钩子以 7 退出
- javascript - vuetifyjs 选择 'hide-selected=true' 显示仅列出前 20 个项目
- python - 有没有办法使用 python 重命名 restAPI 本地服务器中的图像?
- azure - Azure B2C 日期选择器(日历)
- bash - 列出目录中的文件并将它们复制替换到 Linux 中的另一个目录中
- node.js - http POST 上的 net::ERR_SSL_PROTOCOL_ERROR 到快递服务器