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 service is available 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 |
custom | object (opens new window) – the custom config settings |
db | craft\config\DbConfig – the DB config settings |
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 – the general config settings |
loadingConfigFile | string (opens new window), null (opens new window) |
# 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)
# custom
- Type
- object (opens new window)
- Default value
null
the custom config settings
View source (opens new window)
# db
- Type
- craft\config\DbConfig
- Default value
null
the DB config settings
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.
# general
- Type
- craft\config\GeneralConfig
- Default value
null
the general config settings
View source (opens new window)
# loadingConfigFile
- Type
- string (opens new window), null (opens new window)
- Default value
null
- Access
- Read-only
- Since
- 4.2.0
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 a component property. |
__isset() (opens new window) | Checks if a property is set, i.e. defined and not null. |
__set() (opens new window) | Sets the value of a component property. |
__unset() (opens new window) | Sets a component property to be 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. |
getCustom() | Returns the custom config settings. |
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. |
getLoadingConfigFile() | Returns the config filename currently being loaded. |
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 for this component. |
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. |
setBooleanDotEnvVar() | Sets a boolean environment variable value in the project's .env file. |
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
$filename
(string (opens new window)) – The filename (sans .php extension)
Returns
# 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
(string (opens new window))
Returns
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
$category
(string (opens new window)) – The config category
Returns
object (opens new window) – The config settings
Throws
- yii\base\InvalidArgumentException (opens new window)
if $category is invalid - yii\base\InvalidConfigException (opens new window)
if the securityKey general config setting is not set, and a auto-generated one could not be saved
# getCustom()
# getDb()
# getDotEnvPath()
Returns the path to the .env file (regardless of whether it exists).
View source (opens new window)
Returns
# getGeneral()
# getLoadingConfigFile()
- Since
- 4.2.0
Returns the config filename currently being loaded.
View source (opens new window)
Returns
string (opens new window), null (opens new window)
# setBooleanDotEnvVar()
- Since
- 3.7.24
Sets a boolean environment variable value in the project's .env file.
If the environment variable is already set to a boolean-esque value, its counterpart will be used.
For example, if true
is passed and the current value is no
, the variable will be set to yes
.
View source (opens new window)
Arguments
$name
(string (opens new window)) – The environment variable name$value
(boolean (opens new window)) – The environment variable value
Throws
- yii\base\Exception (opens new window)
if the .env file doesn't exist
# setDotEnvVar()
Sets an environment variable value in the project's .env file.
View source (opens new window)
Arguments
$name
(string (opens new window)) – The environment variable name$value
(string (opens new window)) – The environment variable value
Throws
- yii\base\Exception (opens new window)
if the .env file doesn't exist
# Constants
Constant | Description |
---|---|
CATEGORY_CUSTOM | |
CATEGORY_DB | |
CATEGORY_GENERAL |
← Conditions Content →