Getting started
Routing and controllers
Command line
Databases (SQL)
Databases (NoSQL)
Security
Packages
Learn more
- Array helper
- Caching
- Collections
- Command, event and query buses
- File system
- HTML helper
- Humanizer
- Image processing
- Internationalization
- Logging
- Number helper
- Pagination
- Rate limiter
- Retry helper
- Sessions
- String helper
- Time and date handling
- URL builder
- UUID helper
- Validation
- Views
Official packages
Configuration
The Mako core is configured in the app/init.php file. This is where you configure the error reporting level, define the paths to the application and vendor directories, and customize the dotenv loader.
The .env file is located in the application root directory and is intended for local development environments. It should not be committed to version control. By default, values defined in the environment take precedence over values loaded from the .env file, and variable interpolation is disabled.
All remaining application configuration is done by editing the files located in the app/config directory.
Configuration files
Mako configuration files are simple arrays. In addition to the core configuration files created by default, you can create your own custom configuration files:
<?php // app/config/redis.php return [ 'key_1' => 'value', 'key_2' => 'value', ];
Configuration files can be loaded using the get method.
$config = $this->config->get('redis'); // Loads the app/config/redis.php file
You can also fetch configuration items using dot notation. Using the example above, key_1 can be fetched like this:
$default = $this->config->get('redis.key_1');
It is also possible to override settings or add new configuration values at runtime:
// Adds a new Crypto configuration named "user" that you can // use when creating a Crypto instance "Crypto::getInstance('user');" $this->config->set('crypto.configurations.user', [ 'library' => 'openssl', 'cipher' => 'AES-256-OFB', 'key' => mako\env('CRYPTO_KEY_USER'), ]);
Removing the custom configuration is done using the remove method:
$this->config->remove('crypto.configurations.user');
Setting configuration at runtime is not always possible. Some components, such as the connection managers (database, Redis, etc.), cache their settings once they are loaded. You can override them using their
addConfigurationandremoveConfigurationmethods instead.
Security first
Accidentally committing configuration files containing sensitive information is a common security mistake. Public repositories are frequently scanned by automated tools looking for exposed credentials. Take care when deciding which files to commit and which files to add to your .gitignore file or equivalent ignore file for your VCS.
Any configuration files that hold an API key, a username/password, an encryption key or any other type of sensitive data should not be committed to your repository!
Place your sensitive configuration values in environment variables, which can then be accessed using the mako\env function.
'default' => [ 'dsn' => mako\env('DB_DSN'), 'username' => mako\env('DB_USERNAME'), 'password' => mako\env('DB_PASSWORD'), 'persistent' => false, 'log_queries' => false, 'reconnect' => false, 'options' => [ PDO::ATTR_TIMEOUT => 5, ], ],
Environment-aware configuration
Mako supports environment-aware configuration. This means that you can have separate configuration files for your different environments. All you have to do is create a subdirectory named after your environment inside the app/config directory and copy the environment-specific files into it.
Setting the environment
Setting the environment in Apache:
SetEnv MAKO_ENV dev
Setting the environment in Nginx:
fastcgi_param MAKO_ENV dev;
Setting the environment in a linux/unix shell:
export MAKO_ENV=dev # for Bourne, bash, and related shells setenv MAKO_ENV=dev # for csh and related shells
You can also manually set the environment in the CLI using the env option.
php reactor <command> --env=dev
Using the above methods to set up the dev environment, it will now read your configurations from app/config/dev/. If you changed the environment to prod it will pull from app/config/prod/ instead.
Environment variables
You can use the mako\env function to fetch environment variables, which is recommended for sensitive configuration values such as credentials and encryption keys.
[ 'username' => mako\env('USERNAME'), 'password' => mako\env('PASSWORD'), 'doSomething' => mako\env('DO_SOMETHING', as: Type::Bool), ]
You can also specify default values:
[ 'username' => mako\env('USERNAME', 'user'), 'password' => mako\env('PASSWORD', '1234'), 'doSomething' => mako\env('DO_SOMETHING', false, as: Type::Bool), ]
By default, the mako\env function returns a string, but you can cast the return value to other types by using the as parameter.
| Type | Description |
|---|---|
| Type::Bool | Casts the value to TRUE or FALSE |
| Type::Int | Casts the value to an integer |
| Type::Float | Casts the value to a float |
| Type::JsonAsObject | Casts a valid JSON object to an object |
| Type::JsonAsArray | Casts a valid JSON object to an associative array |
Note: If the value cannot be cast to the requested type,
nullor the specified default value will be returned instead.
Package configuration
Check out the package documentation for more information regarding package configuration.