FuelPHP - Configuration



In this chapter, we will understand how to configure a FuelPHP application. By default, configuration files are stored inside the fuel/app/config folder. The application's main configuration is fuel/app/config/config.php. The configuration is specified using PHP's associated array.

Overview

By default, all default configuration files are defined in fuel/core/config folder. To override a default configuration, add the corresponding key in the /fuel/app/config/config.php file and modify the value. We can use “dot-notation” to simplify multi-dimensional array. For example, the following configurations serve the same purpose (load specified packages).

array("always_load" => array("packages" => array( ... ) ) ); 
always_load.packages = array( ... );

Configuration can be grouped by purpose and specified using different files such as db.php for database configuration, package.php for package management, etc.

Type of Configuration Format

FuelPHP is quite flexible and provides different format to specify the configuration. The default configuration format is PHP using php array. The other options are −

INI − Simple text-based configuration supported by many softwares including PHP language itself.

[group]
key = value

YAML − Easy to understand, indentation based, and human readable configuration management.

group:
   key: value

JSON − Easy to understand and most used file format by the developers.

{ 
   "group" : 
   { 
      "key": "value" 
   } 
} 

Memcached − Stores the configuration in a memcached server. The memcached server details can be specified in the main configuration file, fuel/app/config/config.php using config.memcached entry.

DB − Stores the configuration in RDBMS System. The table structure of the configuration table is as follows.

CREATE TABLE IF NOT EXISTS `config` ( 
   `identifier` char(100) NOT NULL, 
   `config` longtext NOT NULL, 
   `hash` char(13) NOT NULL, 
   PRIMARY KEY (`identifier`) 
)

The database and table details can be specified in the configuration file using config.database and config.table_name entries.

Environment

Environment enables FuelPHP to work in different modes by loading different configuration. FuelPHP supports the following environment.

  • Development − \Fuel::DEVELOPMENT sets the development mode

  • Production − \Fuel::PRODUCTION sets the production mode

  • Test − \Fuel::TEST sets the testing mode

  • Staging − \Fuel::STAGING sets the staging mode

FuelPHP also supports the creation of a new environment. This will enable every developer to have his/her own configuration setting and they can enable it while coding and testing the application. The configuration of a specific environment can be added by simply creating a folder with the environment name (example: test) and placing the configuration file inside the newly created folder, shown as follows.

. ├── config.php 
├── db.php 
├── development 
│   └── db.php 
├── production 
│   └── db.php 
├── routes.php 
├── staging 
│   └── db.php
└── test 
    └── db.php  
4 directories, 7 files

Set Your Environment

There are three ways to set up your environment.

Option 1 − Set environment with web server's environment variables. Add the following code in the virtual host section in httpd.conf file of Apache web server. It can be added in .htaccess file as well.

SetEnv FUEL_ENV production

Option 2 − Set environment with FuelPHP bootstrap file, /fuel/app/bootstrap.php

Fuel::$env = (isset($_SERVER['FUEL_ENV']

Option 3 − Set Environment using Oil

$ env FUEL_ENV = production php oil -v

It produces the following result.

Fuel: 1.8 running in "production" mode
Advertisements