The Config component stores the entire merged configuration of your application.
Each module returns its own configuration array from its getConfig()
method; the framework merges these together at boot time using
array_merge_recursive and stores the result in a
Config object. You can then retrieve values by key or needle
anywhere the application object is available.
A config file is a PHP script that returns an array. Here is the top-level config/application.config.php which loads the enabled modules list:
declare(strict_types=1);
return [
'modules' => require __DIR__ . '/modules.config.php',
];
The config/modules.config.php file lists which modules are enabled. Modules are loaded top-to-bottom, and later modules can override values from earlier ones:
declare(strict_types=1);
return [
'Application',
];
Each module defines its own module.config.php inside its config/ directory. This file returns all routes, view manager settings, and any other configuration the module needs:
declare(strict_types=1);
return [
'routes' => [
'application.home' => [
'route' => '/',
'controller' => IndexController::class,
'action' => 'index',
],
],
'view_manager' => [
'template_map' => [
'layout/layout' => 'layout/layout.phtml',
'error/404' => 'error/404.phtml',
],
'template_path_stack' => __DIR__ . '/../view',
],
];
All module configuration is done in module/{Module}/config/module.config.php. The array returned by this file is automatically merged into the global application configuration at boot time.
When the application boots, ApplicationFactory::build() performs
these steps:
getConfig() method.array_merge_recursive() — later modules override earlier ones
when keys collide.Config object.Config to the Application
constructor as $this->config.This means every module contributes to the same configuration namespace.
For example, if two modules both define a view_manager key, their
template_map entries are merged together.
The merged configuration is available on the application object as
$app->config. Values can be retrieved by key using
get() or via the magic __get() method:
declare(strict_types=1);
namespace Application\Controller;
use JiNexus\Mvc\Controller\AbstractController;
use JiNexus\Mvc\Model\ViewModel;
class IndexController extends AbstractController
{
public function indexAction(): ViewModel
{
// Access via get() — returns null if the key does not exist
$templatePath = $this->config->get('view_manager');
// Same, using the magic __get() method
$templatePath = $this->config->view_manager;
// With a default value when the key is missing
$debug = $this->config->get('debug', false);
// Check if a key exists
if ($this->config->has('routes')) {
// …
}
return new ViewModel([
'templatePath' => $templatePath,
]);
}
}
Inside a view template, the $this->config object is also
available because the View holds a reference to the application:
<p><?php echo $this->config->get('timezone', 'UTC'); ?></p>
These are the methods provided by the Config object:
| Method | Description |
|---|---|
getConfig() |
Get the entire configuration array |
setConfig(array $config) |
Replace the entire configuration array |
get($needle, $default = null) |
Get a value by key; returns $default if the key does not exist |
set($needle, $value) |
Set a value by key |
has($needle) |
Returns true if the key exists, false otherwise |
__get($needle) |
Magic method, forwards to get($needle) — enables $config->key syntax |
__set($needle, $value) |
Magic method, forwards to set($needle, $value) |
If you found a typo or error, please help us improve this document. Create Issue