Configuration
Configuration files in Masonite are gathered in one folder named config in default projects.
Each feature can have some options in its own file named after the feature. For example you will find mail related options in config/mail.py file.

Getting Started

The Configuration class is responsible for loading all configuration files before the application starts.
It will load all files located at path defined through config.location binding which default to config/.
Then values are accessed based on the file they belong to and a dotted path can be used to access nested options.
Given the following config/mail.py file:
1
from masonite.environment import env
2
3
FROM_EMAIL = env("MAIL_FROM", "[email protected]")
4
5
DRIVERS = {
6
"default": env("MAIL_DRIVER", "terminal"),
7
"smtp": {
8
"host": env("MAIL_HOST"),
9
"port": env("MAIL_PORT", "587"),
10
"username": env("MAIL_USERNAME"),
11
"password": env("MAIL_PASSWORD"),
12
"from": FROM_EMAIL,
13
}
14
}
Copied!
  • Accessing mail will return a dictionary with all the options.
  • Accessing mail.from_email will return the FROM_EMAIL value
  • Accessing mail.drivers.smtp.port will return the port value for smtp driver.

Getting Value

To read a configuration value one can use the Config facade:
1
from masonite.facades import Config
2
3
Config.get("mail.from_email")
4
# a default value can be provided
5
Config.get("database.mysql.port", 3306)
Copied!
or the config helper:
1
from masonite.configuration import config
2
3
config("mail.from_email")
4
# a default value can be provided
5
config("database.mysql.port", 3306)
Copied!

Setting Value

Setting configuration values is achieved through projet configuration files.

Overriding Value

However, you can override on the fly a configuration value with the Config facade:
1
Config.set("mail.from_email", "[email protected]")
Copied!
This should be done sparingly as this could have unexpected side effects depending at which time you override the configuration option.
This is mostly useful during tests, when you want to override a configuration option to test a specific behaviour:
1
def test_something(self):
2
old_value = Config.get("mail.from_email")
3
Config.set("mail.from_email", "[email protected]")
4
5
# test...
6
7
Config.set("mail.from_email", old_value)
Copied!
But if you simply want to have different configuration depending on the environment (development, testing or production) you should rely instead on environment variables used to define configuration options.