Organizing Configurations with Laravel's Config Repository #719
pronist
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
(I apologize in advance as I am not very fluent in English.)
Jigsaw's configuration is relatively easy since everything can be done in
config.php
, but I wondered if we could manage it more cleanly by separating it, similar to Laravel.Installation
mkdir my-app cd my-app composer require tightenco/jigsaw vendor/bin/jigsaw init npm install
When initializing Jigsaw, the default configuration looks like this:
Configuration
We can categorize the configuration into app settings like
production
,baseUrl
,title
,description
; collections likecollections
; and additional page variables or helper functions. While managing helper functions inhelpers.php
works well, what if we separated the other parts?Let's use Laravel Configuration to separate them.
Since we'll be adding a dedicated class for managing configurations, let's add PSR-4 settings to
composer.json
.Config
Before creating the Configuration class, let's first create a
config
directory and configuration files. We'll separate production-specific settings.config/app.php
config/collections.php
config/variables.php
config/production/app.php
Configuration
The
Configuration
class is a dedicated class for managing settings.In
Configuration
, we use Laravel's Config Repository to manage the settings. We initialize the Repository for the settings, specify the path for the configuration files in the constructor, and load them withConfiguration::load()
. TheConfiguration::getConfig()
method is used inconfig.php
andconfig.production.php
to retrieve the merged settings from the Repository as an array.Now, simply use
Configuration::getConfig()
inconfig.php
andconfig.production.php
.config.php
config.production.php
Beta Was this translation helpful? Give feedback.
All reactions