Skip to content

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.

View source

Public Properties ​

PropertyDescription
appDefaultsDirstring – The path to the directory containing the default application config settings
behaviorsyii\base\Behavior – List of behaviors attached to this component.
configDirstring – The path to the config directory
dbcraft\config\DbConfig – the DB config settings
dotEnvPathstring
envstring, null – The environment ID Craft is currently running in.
generalcraft\config\GeneralConfig – the general config settings

appDefaultsDir ​

Type
string
Default value
''

The path to the directory containing the default application config settings

View source

configDir ​

Type
string
Default value
''

The path to the config directory

View source

db ​

Type
craft\config\DbConfig
Default value
null

the DB config settings

View source

dotEnvPath ​

Type
string
Default value
null
Access
Read-only

View source

env ​

Type
string, null
Default value
null

The environment ID Craft is currently running in.

View source

general ​

Type
craft\config\GeneralConfig
Default value
null

the general config settings

View source

Public Methods ​

MethodDescription
__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.
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()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.

View source

Arguments ​

  • $filename (string) – The filename (sans .php extension)

Returns ​

string

getConfigFromFile() ​

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

View source

Arguments ​

  • $filename (mixed)

Returns ​

array


Example ​

php
// 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

Arguments ​

  • $category (string) – The config category

Returns ​

yii\base\BaseObject – The config settings

Throws ​

getDb() ​

Returns the DB config settings.

View source

Returns ​

craft\config\DbConfig


Example ​

php
$username = Craft::$app->config->db->username;
twig
{% set username = craft.app.config.db.username %}

getDotEnvPath() ​

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

View source

Returns ​

string

getGeneral() ​

Returns the general config settings.

View source

Returns ​

craft\config\GeneralConfig


Example ​

php
$logoutPath = Craft::$app->config->general->logoutPath;
twig
<a href="{{ url(craft.app.config.general.logoutPath) }}">
  Logout
</a>

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

Arguments ​

  • $name (string) – The environment variable name
  • $value (boolean) – The environment variable value

Throws ​

setDotEnvVar() ​

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

View source

Arguments ​

  • $name (string) – The environment variable name
  • $value (string) – The environment variable value

Throws ​

Constants ​

ConstantDescription
CATEGORY_DB
CATEGORY_GENERAL