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 Config service is globally accessible in Craft via Craft::$app->config
.
# Public Properties
Property | Description |
---|---|
appDefaultsDir | string – The path to the directory containing the default application config settings |
behaviors | yii\base\Behavior – List of behaviors attached to this component |
configDir | string – The path to the config directory |
db | craft\config\DbConfig |
dotEnvPath | string |
env | string, null – The environment ID Craft is currently running in. |
general | craft\config\GeneralConfig |
# appDefaultsDir
- Type
- string
- Default value
''
The path to the directory containing the default application config settings
# configDir
- Type
- string
- Default value
''
The path to the config directory
# db
- Type
- craft\config\DbConfig
- Default value
null
- Access
- Read-only
# dotEnvPath
- Type
- string
- Default value
null
- Access
- Read-only
# env
# general
- Type
- craft\config\GeneralConfig
- Default value
null
- Access
- Read-only
# 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 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.
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
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.
Arguments
$category
(string) – The config category
Returns
yii\base\BaseObject – The config settings
Throws
- yii\base\InvalidArgumentException
if $category is invalid - yii\base\InvalidConfigException
if the securityKey general config setting is not set, and a auto-generated one could not be saved
# getDb()
# getDotEnvPath()
Returns the path to the .env file (regardless of whether it exists).
Returns
# getGeneral()
# setDotEnvVar()
Sets an environment variable value in the project's .env file.
Arguments
Throws
- yii\base\Exception
if the .env file doesn't exist
# Constants
Constant | Description |
---|---|
CATEGORY_DB | |
CATEGORY_GENERAL |
← Categories Content →