Give a module its own configuration
To give the Blog module its own settings, edit Modules/Blog/config/config.php or generate extra config files and read them with config(). You need an enabled Blog module. A new module already has Modules/Blog/config/config.php containing 'name' => 'Blog'.
ModuleServiceProvider registers every PHP file in the module’s config/ folder:
| File | Config key prefix |
|---|---|
config/config.php |
blog |
config/settings.php |
blog.settings |
-
Add a setting to
Modules/Blog/config/config.php.Modules/Blog/config/config.php <?phpreturn ['name' => 'Blog','posts_per_page' => 15,]; -
Optional: create a second config file to group related settings.
Terminal window php artisan module:make-config settings BlogThis creates
Modules/Blog/config/settings.php. Add a setting:Modules/Blog/config/settings.php <?phpreturn ['per_page' => 10,]; -
Check the result.
Terminal window php artisan config:show blogblog .......................................................................name .................................................................. Blogposts_per_page .......................................................... 15settings ⇁ per_page ..................................................... 10Read the values with
config('blog.posts_per_page')andconfig('blog.settings.per_page').
Publish the config files
Section titled “Publish the config files”Publish a module’s config files to override values from your application.
-
Publish the config files.
Terminal window php artisan module:publish-config BlogThis creates
config/blog.phpandconfig/blog/settings.php. Add--forceto overwrite existing files. -
Override a value in the published file.
config/blog.php <?phpreturn ['posts_per_page' => 99,]; -
Verify the merged value.
Terminal window php artisan tinker --execute="echo config('blog.posts_per_page');"The output is
99.
Published values override module defaults:
- Associative arrays merge recursively key by key.
- Scalars, lists, and empty arrays (
[]) replace the module’s value as a whole. - Unset keys keep their module defaults, and extra application keys stay intact.
If configuration is cached, run php artisan config:cache after editing a published file. See Config files for the exact merge order.