Development - Configuration Files



All framework configuration files are PHP files. A configuration file contains PHP code returning a named array of parameters as shown below:
<?php
return array(
    // Main settings
    'param1' => 'value1',
    'param2' => 'value2',
    
    // Additional settings
    'param3' => array(
        'param3_1' => 'value3_1',
        'param3_2' => 'value3_2',
        'param3_3' => 'value3_3',
    ),    
    ...
);    

Application configuration files

The application-related configuration files must be placed in protected/config/ subdirectory inside the application directory. Changing the location or the name of a configuration file is not allowed.

File main.php

File main.php is the main configuration file of the application. It contains the application name, current version number, default template, controller, action and other parameters.

Example of the main.php file for Login System application:
<?php
return array(
    // Application data
    'name'=>'Login System',
    'version'=>'0.0.1',

    // Installation settings
    'installationKey' => 'YOUR_INSTALLATION_KEY',

    // Password keys settings (for database passwords only - don't change it)
    // md5, sha1, sha256, whirlpool, etc.
    'password' => array(
        'encryption' => true,
        'encryptAlgorithm' => 'sha256',
        'encryptSalt' => true,
        'hashKey' => 'YOUR_HASH_PASSWORD_KEY',    
    ),
    
    // Default email settings 
    'email' => array(
        'mailer' => 'smtpMailer', /* phpMail | phpMailer | smtpMailer */
        'from' => 'info@email.me',
        'fromName' => '', /* John Smith */
        'isHtml' => true,
        'smtp' => array(
            'auth' => true, /* true or false */
            'secure' => 'ssl', /* 'ssl', 'tls' or '' */
            'host' => 'smtp.gmail.com',
            'port' => '465',
            'username' => '',
            'password' => '',
        ),
    ),
    
    // Validation
    'validation' => array(
        'csrf' => true
        'bruteforce' => array('enable' => true, 'badLogins' => 5, 'redirectDelay' => 3),
    ),

    // Exception handling
    // Define exceptions exceptions in application
    'exceptionHandling' => array(
        'enable' => true, 
        'level' => 'global'
    ),

    // Output compression
    'compression' => array(
		'gzip' => array('enable' => true),
		'html' => array('enable' => false),
    ),

    // Session settings
    'session' => array(
        'customStorage' => false, 	/* true value means use a custom storage (database), false - standard storage */
        'cacheLimiter' => '',		/* to prevent 'Web Page expired' message for POST request use "private,must-revalidate" */
        'lifetime' => 24,		/* session timeout in minutes, default: 24 min = 1440 sec */
    ),
    
    // Cookies settings
    'cookies' => array(
        'domain' => '', 
        'path' => '/' 
    ),

    // Cache settings 
    'cache' => array(
        'enable' => false,
        'type' => 'auto', /* 'auto' or 'manual' */
        'lifetime' => 20, /* in minutes */
        'path' => 'protected/tmp/cache/'
    ),

    // Logger settings 
    'log' => array(
        'enable' => true, 
        'path' => 'protected/tmp/logs/',
        'fileExtension' => 'php', 	
        'dateFormat' => 'Y-m-d H:i:s',
        'threshold' => 1,
        'filePermissions' => 0644,
        'lifetime' => 30			/* in days */
    ),

    // RSS Feed settings 
    'rss' => array(
        'path' => 'feeds/'
    ),

    // Datetime settings
    'defaultTimeZone' => 'UTC',
    
    // Template default settings  
    'template' => array(
        'default' => 'default'
    ),
    
    // Layout default settings  
    'layouts' => array(
        'enable' => true, 
        'default' => 'default'
    ),
    
    // Default settings (optional, if defined - will be used as application default settings)
    'defaultErrorController' => 'Error', /* may be overridden by module settings */
    'defaultController' => 'Index',		 /* may be overridden by module settings */
    'defaultAction' => 'index',			 /* may be overridden by module settings */
    
    // Application payment complete page (controller/action - may be overridden by module settings)
    'paymentCompletePage' => '',

    // Application components
    'components' => array(
        'Bootstrap' => array('enable' => true, 'class' => 'Bootstrap'),
        // Other components...
    ),

    // Widget settings
    'widgets' => array(
        'paramKeysSensitive' => false
    ),

    // Application helpers
    'helpers' => array(
        //'helper' => array('enable' => true, 'class' => 'Helper'),
        // Other helpers...
    ),

    // Application modules
    'modules' => array(
        'setup' => array('enable' => true, 'removable' => false),
        // Other modules...
    ),

    // Url manager
    'urlManager' => array(
        'urlFormat' => 'shortPath',  /* get | path | shortPath */
        'rules' => array(
            //'controller/action/value1/value2' => 'controller/action/param1/value1/param2/value2',
        ),
    ),    
);
Below the list of all parameters that can be used in file main.php:

File db.php

File db.php contains database connection parameters.

Example of the db.php file for Login System application:
<?php
return array(
    // Database settings
    'db' => array(
        'driver' 	=> 'mysql',
        'socket' 	=> '',
        'host' 		=> 'localhost',
        'port' 		=> '',
        'database' 	=> 'mvc',
        'username' 	=> 'root',
        'password' 	=> '',
        'prefix' 	=> 'apcmf_',
        'charset' 	=> 'utf8',
        'schema' 	=> 'public'
    ),       
);
Below the list of all parameters that can be used in file db.php:

Examples of using configuration in application code

Setting up mailer configuration:
CMailer::config(array('mailer'=>CConfig::get('email.mailer')));
Setting up db connection:
parent::__construct(
    CConfig::get('db.driver').':host='.CConfig::get('db.host').';dbname='.CConfig::get('db.database'),
    CConfig::get('db.username'),
    CConfig::get('db.password')
);

Module Files

ApPHP framework allows to export while installation module configuration files in config directory. This feature allows application to merge them and use like a one configuration file. To read more info click here.