4. Configuration

The config/ directory holds the general configuration of the project. The Config component provides a nested object property based user interface for accessing such configuration data within application. The Registry component acts as a container for storing configuration values.

Trex uses PHP configuration files.

“If you are writing a component you then leave it up to your user whether to use the programmatic interface, your configuration file format, or to write their own custom configuration file format and tie it into the programmatic interface” - Martin Fowler

4.1.1. Database Configuration

Your DatabaseConfig.php file is where your database configuration all takes place. Open the file with a text editor and you’ll see the following:

config/DatabaseConfig.php

public function getConfigArray()
{
        // Default Database Connection
        $databaseArray["default"]["host"] = "127.0.0.1";
        $databaseArray["default"]["user"] = "username";
        $databaseArray["default"]["pass"] = "password";
        $databaseArray["default"]["name"] = "framework";

        // Database configuration
        $config["database"]["selected"] = "default";
        $config["database"]["adapter"] = "mysqli";
        $config["database"]["connection"] = $databaseArray;

        return $config;
}

Replace the information provided by default with the database connection information for your application.

4.1.2. Debugger Configuration

The Debug component helps you debugging your script by providing a lot of valuable debug information. If you want to allow access to your debug log only for an IP address or a range of IP addresses, you can set it in your DebugConfig.php file.

config/DebugConfig.php

public function getConfigArray()
{
        // Allow access by IP address or IP range
        $config["debug"]["enabled"] = true;
        $config["debug"]["ipallowed"]["local"]  = "127.0.0.1";
        $config["debug"]["ipallowed"]["developers"]  = "199.4.23.*";
        $config["debug"]["ipallowed"]["client"]  = "86.141.182.102";

        return $config;
}

4.1.3. Mail Configuration

The MailConfig.php file is where your Mail configuration is stored:

config/MailConfig.php

public function getConfigArray()
{
        // Email accounts
        $emailAccount["default"] = array("name"=>"Default", "email"=>"default@domain.com");
        $emailAccount["test"] = array("name"=>"Test", "email"=>"test@domain.com");

        // SMTP (optional)
        $smtpConnection["host"] = "smtp.domain.com";
        $smtpConnection["port"] = 25;
        $smtpConnection["username"] = "username";
        $smtpConnection["password"] = "password";

        // Set Mail configuration
        $config["mail"]["smtp"] = $smtpConnection;
        $config["mail"]["account"] = $emailAccount;
        $config["mail"]["selected"] = "default";

        return $config;
}

4.2. Accessing Configuration Values

You can access settings from within the application code through the Registry class. The Registry is a mechanism for providing singleton instances of values to the application space. Trex stores the Config object in the Registry once and then retrieves it whenever it is needed.

Example: Within a module

// retrieve a copy of the debug configuration
$debugConfigObj = $this->config()->debug;

// echo $debugConfigObj->enabled will output "1"
echo $debugConfigObj->enabled;

// echo $debugConfigObj->ipallowed->local will output "127.0.0.1"
echo $debugConfigObj->ipallowed->local;

// output an associative array of the stored data
print_r($debugConfigObj->asArray());

The above example will output:

1
127.0.0.1
Array
(
    [enabled] => 1
    [ipallowed] => Array
        (
            [local] => 127.0.0.1
            [developers] => 199.4.23.*
            [client] => 86.141.182.102
        )
)