简体   繁体   English

如何使包配置将其不存在的键合并到 Laravel 7.x 中的应用程序配置文件中?

[英]How can I make the package config merge it's non-existing keys into the app config file in laravel 7.x?

I'm developing some packages that make my app more modular and allow the user to enable/disable them on the fly, or install additional packages when needed.我正在开发一些软件包,使我的应用程序更加模块化,并允许用户即时启用/禁用它们,或者在需要时安装其他软件包。

Some of these packages will require the user to configure certain properties (such as api-keys), for this reason I'd like the packages to write their config keys & values to the <app-root>/config/packages.php file (if the keys don't exist yet).其中一些包将要求用户配置某些属性(例如 api-keys),因此我希望这些包将它们的配置键和值写入<app-root>/config/packages.php文件(如果密钥尚不存在)。

From what I understand from the laravel documentation (albeit a bit vague) I can use either:根据我从 laravel 文档中的理解(虽然有点模糊),我可以使用:

$this->mergeConfigFrom(__DIR__.'/config/config.php', 'packages');

or或者

$this->publishes([
    __DIR__.'/config/config.php' => config_path('packages.php')
]);

in the register() method of my package ServiceProvider class.在我的包ServiceProvider类的register()方法中。

Additionally I also added an echo "called";此外,我还添加了一个echo "called";echo "called"; to the register() method so I can check if the code is actually executed.register()方法,以便我可以检查代码是否实际执行。

Next I run compose dump-autoload , I see called in the log output, but when I check the <app-root>/config/packages.php file it still has the empty array.接下来我运行compose dump-autoload ,我在日志输出中看到called了,但是当我检查<app-root>/config/packages.php文件时,它仍然有空数组。

Using php artisan config:clear and then running compose dump-autoload once more doesn't make a difference either.使用php artisan config:clear然后再次运行compose dump-autoload也没有什么区别。

How can I make the package config merge it's non-existing keys into the <app-root>/config/packages.php file?如何使包配置将其不存在的键合并到<app-root>/config/packages.php文件中?

This is an example of my package config file:这是我的包配置文件的示例:

<?php

return [
    'shoutzor' => [
        'acoustid' => [
            'apikey'  => env('SHOUTZOR_ACOUSTID_APIKEY', '')
        ]
    ]
];

and my <app-root>/config/packages.php file contains:我的<app-root>/config/packages.php文件包含:

<?php

return [];

You (or the user) need to run the command php artisan vendor:publish before the package's configuration file will be published (copied in the config folder, or the folder specified)您(或用户)需要在php artisan vendor:publish包的配置文件之前运行命令php artisan vendor:publish (复制到config文件夹中,或指定的文件夹中)

From the documentation文档

Now, when users of your package execute Laravel's vendor:publish command, your file will be copied to the specified publish location.现在,当你的包的用户执行 Laravel 的vendor:publish命令时,你的文件将被复制到指定的发布位置。

If you want to publish a specific package vendor files, you will just need to pass the package service provider as option to the command:如果你想发布一个特定的包供应商文件,你只需要将包服务提供者作为选项传递给命令:

php artisan vendor:publish --provider="Your\Package\ServiceProvider"

Important If the files to publish already exist in the destination folder, you will need to run the command with the --force option:重要如果要发布的文件已存在于目标文件夹中,则需要使用--force选项运行命令:

php artisan vendor:publish --force

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM