Config ​
- Type
- Class
- Namespace
- craft\services
- Inherits
- craft\services\Config » yii\base\Component » yii\base\BaseObject
- Implements
- yii\base\Configurable
- Since
- 3.0.0
The Config service provides APIs for retrieving the values of Craft’s config settings, as well as the values of any plugins’ config settings.
An instance of the service is available via Craft::$app->config
.
Public Properties ​
Property | Description |
---|---|
appDefaultsDir | string – The path to the directory containing the default application config settings |
appType | string – The application type (web or console ). |
behaviors | yii\base\Behavior – List of behaviors attached to this component. |
configDir | string – The path to the config directory |
custom | object – the custom config settings |
db | craft\config\DbConfig – the DB config settings |
dotEnvPath | string |
env | string, null – The environment ID Craft is currently running in. |
general | craft\config\GeneralConfig – the general config settings |
loadingConfigFile | string, null |
appDefaultsDir
​
- Type
- string
- Default value
''
The path to the directory containing the default application config settings
appType
​
- Type
- string
- Default value
null
The application type (web
or console
).
configDir
​
- Type
- string
- Default value
''
The path to the config directory
custom
​
- Type
- object
- Default value
null
the custom config settings
db
​
- Type
- craft\config\DbConfig
- Default value
null
the DB config settings
dotEnvPath
​
- Type
- string
- Default value
null
- Access
- Read-only
env
​
The environment ID Craft is currently running in.
Example ​
$env = Craft::$app->config->env;
{% if craft.app.config.env == 'production' %}
{% include "_includes/ga" %}
{% endif %}
general
​
- Type
- craft\config\GeneralConfig
- Default value
null
the general config settings
loadingConfigFile
​
Public Methods ​
Method | Description |
---|---|
__call() | Calls the named method which is not a class method. |
__clone() | This method is called after the object is created by cloning an existing one. |
__construct() | Constructor. |
__get() | Returns the value of a component property. |
__isset() | Checks if a property is set, i.e. defined and not null. |
__set() | Sets the value of a component property. |
__unset() | Sets a component property to be null. |
attachBehavior() | Attaches a behavior to this component. |
attachBehaviors() | Attaches a list of behaviors to the component. |
behaviors() | Returns a list of behaviors that this component should behave as. |
canGetProperty() | Returns a value indicating whether a property can be read. |
canSetProperty() | Returns a value indicating whether a property can be set. |
className() | Returns the fully qualified name of this class. |
detachBehavior() | Detaches a behavior from the component. |
detachBehaviors() | Detaches all behaviors from the component. |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. |
getBehavior() | Returns the named behavior object. |
getBehaviors() | 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() | Returns a value indicating whether there is any handler attached to the named event. |
hasMethod() | Returns a value indicating whether a method is defined. |
hasProperty() | Returns a value indicating whether a property is defined for this component. |
init() | Initializes the object. |
off() | Detaches an existing event handler from this component. |
on() | 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() | Triggers an event. |
getConfigFilePath()
​
Returns the path to a config file.
Arguments ​
$filename
(string) – 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.
Arguments ​
$filename
(string)
Returns ​
array, callable, craft\config\BaseConfig
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.
Arguments ​
$category
(string) – The config category
Returns ​
object – The config settings
Throws ​
- yii\base\InvalidArgumentException
if $category is invalid
getCustom()
​
- Since
- 4.0.0
Returns the custom config settings.
Returns ​
Example ​
$myCustomSetting = Craft::$app->config->custom->myCustomSetting;
{% set myCustomSetting = craft.app.config.custom.myCustomSetting %}
getDb()
​
Returns the DB config settings.
Returns ​
Example ​
$username = Craft::$app->config->db->username;
{% set username = craft.app.config.db.username %}
getDotEnvPath()
​
Returns the path to the .env file (regardless of whether it exists).
Returns ​
getGeneral()
​
Returns the general config settings.
Returns ​
Example ​
$logoutPath = Craft::$app->config->general->logoutPath;
<a href="{{ url(craft.app.config.general.logoutPath) }}">
Logout
</a>
getLoadingConfigFile()
​
- Since
- 4.2.0
Returns the config filename currently being loaded.
Returns ​
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
.
Arguments ​
Throws ​
- yii\base\Exception
if the .env file doesn't exist
setDotEnvVar()
​
Sets an environment variable value in the project's .env
file.
Arguments ​
$name
(string) – The environment variable name$value
(string, false) – The environment variable value, orfalse
if it should be removed.
Throws ​
- yii\base\Exception
if the .env file doesn't exist
Constants ​
Constant | Description |
---|---|
CATEGORY_CUSTOM | |
CATEGORY_DB | |
CATEGORY_GENERAL |