Config Class

The Config class provides a unified interface for storing and retrieving configuration values across your application. Settings can come from the default config file, your own custom config files, or be set dynamically at runtime.

Config Files

LavaLust’s primary config file is located at app/config/config.php. All configuration values are stored in a PHP array named $config:

<?php
// app/config/config.php
$config['base_url']        = 'https://yourdomain.com/';
$config['default_language'] = 'english';
$config['charset']         = 'UTF-8';

You can add your own keys directly to this file, or create separate config files in the same app/config/ folder to keep related settings grouped together. A custom config file follows the same format:

<?php
// app/config/blog_settings.php
$config['site_name']    = 'My Blog';
$config['posts_per_page'] = 10;
$config['allow_comments'] = TRUE;

Note

Custom config files must use the same $config array name. LavaLust merges files intelligently, but index names must be unique across all config files to avoid collisions. If a collision is likely, use indexed loading — see Loading Config Files below.

Loading Config Files

Autoloading (recommended for global settings)

If a config file is needed throughout the application, register it in app/config/autoload.php so it is loaded automatically on every request:

<?php
// app/config/autoload.php
$autoload['config'] = ['blog_settings', 'payment'];

Manual loading (recommended for route-specific settings)

Load a config file inside a controller when it is only needed for specific requests. This avoids loading unnecessary configuration on every page.

<?php
$this->config->load('blog_settings');

By default, all items from the loaded file are merged into the global config array. If two config files share an index name, the later-loaded file will overwrite the earlier one.

Indexed loading (avoids key collisions)

Pass TRUE as the second argument to store the file’s items in their own named index instead of merging them into the global array:

<?php
// Items stored under $config['blog_settings'][...]
// instead of being merged into the root config array
$this->config->load('blog_settings', TRUE);

Method

Signature

Description

load()

load($file, $use_sections = FALSE)

Load a config file. Set $use_sections to TRUE to store items under a named index and prevent key collisions.

Fetching Config Items

Use get() to read any config value by its array key.

Basic retrieval

<?php
$charset  = $this->config->get('charset');
$base_url = $this->config->get('base_url');

Returns NULL if the key does not exist.

Retrieving from an indexed section

If the config file was loaded with $use_sections = TRUE, pass the section name as the second argument:

<?php
$this->config->load('blog_settings', TRUE);

// Read a value from the 'blog_settings' section
$site_name = $this->config->get('site_name', 'blog_settings');

// Or retrieve the entire section as an array and access keys manually
$blog = $this->config->get('blog_settings');
$site_name = $blog['site_name'];

Using the config_item() helper

For convenience, LavaLust also provides a global helper function that reads from the root config array without needing $this->config:

<?php
// Equivalent to $this->config->get('base_url')
$base_url = config_item('base_url');

This is especially useful inside helpers, models, and middleware where $this->config is not directly available.

Method

Signature

Description

get()

get($item, $index = '')

Retrieve a config value by key, optionally from a named section

config_item()

config_item($item)

Global helper shortcut for root-level config values

Setting Config Items at Runtime

Use set() to add a new config value or override an existing one dynamically during the current request. Changes made this way are not written back to the config file on disk and only apply for the lifetime of the request.

<?php
// Override an existing value
$this->config->set('default_language', 'filipino');

// Add a new value at runtime
$this->config->set('maintenance_mode', TRUE);

// Read it back immediately
$lang = $this->config->get('default_language'); // 'filipino'

Method

Signature

Description

set()

set($item, $value)

Set or override a config value for the current request only

Quick Reference

Task

Code

Load a config file (merged)

$this->config->load('filename')

Load a config file (indexed)

$this->config->load('filename', TRUE)

Autoload a config file

Add 'filename' to $autoload['config'] in autoload.php

Read a root-level value

$this->config->get('key')

Read from an indexed section

$this->config->get('key', 'section_name')

Read using the global helper

config_item('key')

Set or override a value

$this->config->set('key', 'value')

Tips and Best Practices

  • Group related settings into their own config file (e.g. payment.php, social.php) rather than adding everything to config.php. This keeps files short and easy to scan.

  • Use indexed loading (TRUE second argument) whenever a custom config file could share key names with another file or with config.php.

  • Use config_item() inside helpers and models — it is shorter and works everywhere $this->config does not.

  • Never store secrets (API keys, passwords) directly in config files that are committed to version control. Load them from environment variables instead:

    <?php
    $config['db_password'] = getenv('DB_PASSWORD');