Skip to main content

Config

Setting settings

Config

Have it just the way you like it!

Introduction

Configuring Morningmedley is done with a global configuration object.

Every module can add its own default configuration to this object, but any default can be overwritten and in some cases extended.

In the root of your project you will find a /config directory.

Here is where all your configs live. Whenever we in these docs reference the config files, these are what we mean.

At runtime every module will add their own default configs to the config repository and every config file will be merged into it.

Basic usage

To access the config object use the config function.

$config = config(); // Returns the config repository object

Reading the config

To read the entire config use config()->all().

$config = config()->all();

To get a specific value pass the key as a string to the function. This accepts nested keys in dot notation and an optional default value to return when key does not exist.

$appConfig = config('app');
$provider = config('app.providers');
// With default []
$provider = config('app.providers',[]);

Adding configurations

To add to the config at runtime use config function with an assoc array.

config(['foo' => 'bar']); // Here the foo config will have "bar" as its value

If a key already exists and is an array then you can push additional values to it.

config('block.paths')->push(__DIR__ . "/blocks"); // Here the foo config will have "bar" as its value

Every *.php file's return value in the /config directory will be added to the config object as well.

So to add the same 'foo' => 'bar' example as above via a file you would do it like so:

/config/foo.php
<?php

return 'bar';

Overwriting configurations

To replace an existing value either replace it at runtime or redefine it in a config file.

This works the same as above.

Caching

To create or update a cached config object use:

wp artisan config:cache

This creates /bootstrap/cache/config.php. If this file exists its contents will be used as config and config generation in service providers will be skipped. Make sure to call the command when deploying to production.

Environments

You can store sensitive data or configurations such as API tokens in a .env file. If a valid environment file exists it will be loaded and available through the env(key) function.

If a file called .env or .env.environment, where "environment is your current WP_ENVIRONMENT_TYPE, it will be loaded.

You can then load these variables in a config file for use throughout the application. This way, these values are also cached.

Example

Here we add an API token for production and name it "API_TOKEN":

.env.production
API_TOKEN=1234VERYSECURE

Then we can assign this to our app config:

config/app.php
<?php return [
'api_token' => env('API_TOKEN'),
];

And lastly, we can now read the value in our application:

$apiToken = config('app.api_token');

Modules

Modules merges their configs during the register step when the App object is constructed.

This way their configs can get cached. This is especially important when their configs are expensive to generate.

Every module uses its own name as a toplevel key in the config object and so to overwrite them create a config file named the same as the module and replace the values you need to.

To read more about each module go to Building Your App