Config

Type
Class
Namespace
craft\services
Inherits
craft\services\Config » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.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 Config service is globally accessible in Craft 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
$dotEnvPathstring
$envstring, null – The environment ID Craft is currently running in.
$generalcraft\config\GeneralConfig

$appDefaultsDir

Type
string

The path to the directory containing the default application config settings

View source

Signature

public string $appDefaultsDir = ''

$configDir

Type
string

The path to the config directory

View source

Signature

public string $configDir = ''

$db

Signature

public craft\config\DbConfig getDb ( )

$dotEnvPath

Type
string
Access
Read-only

View source

Signature

public string getDotEnvPath ( )

$env

Type
string, null

The environment ID Craft is currently running in.

View source

Signature

public string, null $env = null

Example

$general

Signature

public craft\config\GeneralConfig getGeneral ( )

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 an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to 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.
init()Initializes the object.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
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

Signature

public string getConfigFilePath ( \craft\services\string $filename )

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

Returns

array

Signature

public array getConfigFromFile ( \craft\services\string $filename )

Example

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

Signature

public yii\base\BaseObject getConfigSettings ( \craft\services\string $category )

getDb()

Returns the DB config settings.

View source

Returns

craft\config\DbConfig

Signature

public craft\config\DbConfig getDb ( )

Example

getDotEnvPath()

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

View source

Returns

string

Signature

public string getDotEnvPath ( )

getGeneral()

Returns the general config settings.

View source

Returns

craft\config\GeneralConfig

Signature

public craft\config\GeneralConfig getGeneral ( )

Example

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

Signature

public void setDotEnvVar ( $name, $value )

Constants

ConstantDescription
CATEGORY_DB
CATEGORY_GENERAL