Config

Type
Class
Namespace
craft\services
Inherits
craft\services\Config » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Since
3.0.0

The Config service provides APIs for retrieving the values of Craft’s config settings (opens new window), as well as the values of any plugins’ config settings.

An instance of the Config service is globally accessible in Craft via Craft::$app->config.

View source (opens new window)

# Public Properties

Property Description
appDefaultsDir string (opens new window) – The path to the directory containing the default application config settings
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component
configDir string (opens new window) – The path to the config directory
db craft\config\DbConfig
dotEnvPath string (opens new window)
env string (opens new window), null (opens new window) – The environment ID Craft is currently running in.
general craft\config\GeneralConfig

# appDefaultsDir

Type
string (opens new window)
Default value
''

The path to the directory containing the default application config settings

View source (opens new window)

# configDir

Type
string (opens new window)
Default value
''

The path to the config directory

View source (opens new window)

# db

Type
craft\config\DbConfig
Default value
null
Access
Read-only

View source (opens new window)

# dotEnvPath

Type
string (opens new window)
Default value
null
Access
Read-only

View source (opens new window)

# env

Type
string (opens new window), null (opens new window)
Default value
null

The environment ID Craft is currently running in.

View source (opens new window)

Example

$env = Craft::$app->config->env;

# general

Type
craft\config\GeneralConfig
Default value
null
Access
Read-only

View source (opens new window)

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of an object property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets value of an object property.
__unset() (opens new window) Sets an object property to null.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
className() (opens new window) Returns the fully qualified name of this class.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getConfigFilePath() Returns the path to a config file.
getConfigFromFile() Loads a config file from the config/ folder, checks if it's a multi-environment config, and returns the values.
getConfigSettings() Returns all of the config settings for a given category.
getDb() Returns the DB config settings.
getDotEnvPath() Returns the path to the .env file (regardless of whether it exists).
getGeneral() Returns the general config settings.
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined.
init() (opens new window) Initializes the object.
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
setDotEnvVar() Sets an environment variable value in the project's .env file.
trigger() (opens new window) Triggers an event.

# getConfigFilePath()

Returns the path to a config file.

View source (opens new window)

Arguments

Returns

string (opens new window)

# getConfigFromFile()

Loads a config file from the config/ folder, checks if it's a multi-environment config, and returns the values.

View source (opens new window)

Arguments

  • $filename

Returns

array (opens new window)

Example

// get the values defined in config/foo.php
$settings = Craft::$app->config->getConfigFromFile('foo');

# getConfigSettings()

Returns all of the config settings for a given category.

View source (opens new window)

Arguments

Returns

yii\base\BaseObject (opens new window) – The config settings

Throws

# getDb()

Returns the DB config settings.

View source (opens new window)

Returns

craft\config\DbConfig

Example

$username = Craft::$app->config->db->username;

# getDotEnvPath()

Returns the path to the .env file (regardless of whether it exists).

View source (opens new window)

Returns

string (opens new window)

# getGeneral()

Returns the general config settings.

View source (opens new window)

Returns

craft\config\GeneralConfig

Example

$logoutPath = Craft::$app->config->general->logoutPath;

# setDotEnvVar()

Sets an environment variable value in the project's .env file.

View source (opens new window)

Arguments

Throws

# Constants

Constant Description
CATEGORY_DB
CATEGORY_GENERAL