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).
From what I understand from the laravel documentation (albeit a bit vague) I can use either:
$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.
Additionally I also added an echo "called";
to the register()
method so I can check if the code is actually executed.
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.
Using php artisan config:clear
and then running compose dump-autoload
once more doesn't make a difference either.
How can I make the package config merge it's non-existing keys into the <app-root>/config/packages.php
file?
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:
<?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)
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.
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:
php artisan vendor:publish --force
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.