ConfigService

Type
Class
Namespace
Craft
Inherits
Craft\ConfigService » Craft\BaseApplicationComponent » CApplicationComponent » CComponent
Implements
IApplicationComponent
Since
1.0

ConfigService provides APIs for retrieving the values of Craft’s config settings, as well as the values of any plugins’ config settings.

An instance of ConfigService is globally accessible in Craft via {@link WebApp::config craft()->config}.

See also http://craftcms.com

View source

Public Properties

PropertyDescription
$behaviorsarray – The behaviors that should be attached to this component.

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__get()Returns a property value, an event handler list or a behavior based on its name.
__isset()Checks if a property value is null.
__set()Sets value of a component property.
__unset()Sets a component property to be null.
allowAutoUpdates()Returns whether the system is allowed to be auto-updated to the latest release.
asa()Returns the named behavior object.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
attachEventHandler()Attaches an event handler to an event.
canGetProperty()Determines whether a property can be read.
canSetProperty()Determines whether a property can be set.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
detachEventHandler()Detaches an existing event handler.
disableBehavior()Disables an attached behavior.
disableBehaviors()Disables all behaviors attached to this component.
enableBehavior()Enables an attached behavior.
enableBehaviors()Enables all behaviors attached to this component.
evaluateExpression()Evaluates a PHP expression or callback under the context of this component.
exists()Returns whether a config setting value exists, by a given name.
get()Returns a config setting value by its name.
getCacheDuration()Returns the value of the cacheDuration config setting, normalized into seconds.
getCpLoginPath()Returns the path to the CP’s Login page.
getCpLogoutPath()Returns the path to the CP’s Logout page.
getDbItem()Returns a config setting value by its name, pulled from craft/config/db.php.
getElevatedSessionDuration()Returns the configured elevated session duration in seconds.
getEventHandlers()Returns the list of attached event handlers for an event.
getIsInitialized()Checks if this application component has been initialized.
getLocalized()Returns a localized config setting value by its name.
getLoginPath()Returns the user login path based on the type of the current request.
getLogoutPath()Returns the user logout path based on the type of the current request.
getResourceTrigger()Returns the Resource Request trigger word based on the type of the current request.
getUserSessionDuration()Returns the configured user session duration in seconds, or null if there is none because user sessions should expire when the HTTP session expires.
hasEvent()Determines whether an event is defined.
hasEventHandler()Checks whether the named event has attached handlers.
hasProperty()Determines whether a property is defined.
init()Initializes the application component.
isInitialized()Checks if this application component has been initialized yet, or not.
maxPowerCaptain()Sets PHP’s memory limit to the maximum specified by the phpMaxMemoryLimit config setting, and gives the script an unlimited amount of time to execute.
omitScriptNameInUrls()Returns whether generated URLs should omit “index.php”, taking the omitScriptNameInUrls config setting value into account.
parseEnvironmentString()Parses a string for any environment variables.
raiseEvent()Raises an event.
set()Overrides the value of a config setting to a given value.
usePathInfo()Returns whether generated URLs should be formatted using PATH_INFO, taking the usePathInfo config setting value into account.

allowAutoUpdates()

Returns whether the system is allowed to be auto-updated to the latest release.

View source

Returns

boolean

Signature

public boolean allowAutoUpdates ( )

exists()

Returns whether a config setting value exists, by a given name. If the config file is set up as a multi-environment config, only values from config arrays that match the current request’s environment will be checked.

By default, exists() will check craft/config/general.php, and fall back on the default values specified in craft/app/etc/config/defaults/general.php. See Craft’s documentation for a full list of config settings that Craft will check for within that file.

If you want to get the config setting from a different config file (e.g. config/myplugin.php), you can specify its filename as a second argument. If the filename matches a plugin handle, get() will check for a craft/plugins/PluginHandle]/config.php file and use the array it returns as the list of default values.

if (craft()->config->exists('myConfigSetting', 'myplugin'))
{
    Craft::log('This site has some pretty useless config settings.');
}

View source

Arguments

  • $item (string) – The name of the config setting.
  • $file (string) – The name of the config file (sans .php). Defaults to 'general'.

Returns

boolean – Whether the config setting value exists.

Signature

public boolean exists ( $item, $file = \Craft\ConfigFile::General )

get()

Returns a config setting value by its name. If the config file is set up as a multi-environment config, only values from config arrays that match the current request’s environment will be checked and returned.

By default, get() will check craft/config/general.php, and fall back on the default values specified in craft/app/etc/config/defaults/general.php. See Craft’s documentation for a full list of config settings that Craft will check for within that file.

$isDevMode = craft()->config->get('devMode');

If you want to get the config setting from a different config file (e.g. config/myplugin.php), you can specify its filename as a second argument. If the filename matches a plugin handle, get() will check for a craft/plugins/PluginHandle]/config.php file and use the array it returns as the list of default values.

$myConfigSetting = craft()->config->get('myConfigSetting', 'myplugin');

View source

Arguments

  • $item (string) – The name of the config setting.
  • $file (string) – The name of the config file (sans .php). Defaults to 'general'.

Returns

mixed – The value of the config setting, or null if a value could not be found.

Signature

public mixed get ( $item, $file = \Craft\ConfigFile::General )

getCacheDuration()

Returns the value of the cacheDuration config setting, normalized into seconds. The actual value of the cacheDuration config setting is supposed to be set using the PHP interval specification.

craft()->config->get('cacheDuration'); // 'P1D'
craft()->config->getCacheDuration();   // 86400

View source

Returns

integer – The cacheDuration config setting value, in seconds.

Signature

public integer getCacheDuration ( )

getCpLoginPath()

Returns the path to the CP’s Login page.

View source

Returns

string – The Login path.

Signature

public string getCpLoginPath ( )

getCpLogoutPath()

Returns the path to the CP’s Logout page.

View source

Returns

string – The Logout path.

Signature

public string getCpLogoutPath ( )

getDbItem()

DEPRECATED

Deprecated Deprecated in 2.0. Use {@link get() get('key', ConfigFile::Db)} instead.

Returns a config setting value by its name, pulled from craft/config/db.php.

View source

Arguments

  • $item (string) – The name of the config setting.
  • $default (mixed) – The default value to be returned in the event that the config setting isn’t set. Defaults to null.

Returns

string – The value of the config setting, or $default if a value could not be found.

Signature

public string getDbItem ( $item, $default = null )

getElevatedSessionDuration()

Returns the configured elevated session duration in seconds.

View source

Returns

integer, boolean – The elevated session duration in seconds or false if it has been disabled.

Signature

public integer, boolean getElevatedSessionDuration ( )

getLocalized()

Returns a localized config setting value by its name. Internally, {@link get()} will be called to get the value of the config setting. If the value is an array, then only a single value in that array will be returned: the one that has a key matching the $localeId argument. If no matching key is found, the first element of the array will be returned instead.

This function is used for Craft’s “localizable” config settings:

View source

Arguments

  • $item (string) – The name of the config setting.
  • $localeId (string) – The locale ID to return. Defaults to {@link WebApp::language craft()->language}.
  • $file (string) – The name of the config file (sans .php). Defaults to 'general'.

Returns

mixed – The value of the config setting, or null if a value could not be found.

Signature

public mixed getLocalized ( $item, $localeId = null, $file = \Craft\ConfigFile::General )

getLoginPath()

Returns the user login path based on the type of the current request. If it’s a front-end request, the loginPath config setting value will be returned. Otherwise the path specified in {@link getCpLoginPath()} will be returned.

View source

Returns

string – The login path.

Signature

public string getLoginPath ( )

getLogoutPath()

Returns the user logout path based on the type of the current request. If it’s a front-end request, the logoutPath config setting value will be returned. Otherwise the path specified in {@link getCpLogoutPath()} will be returned.

View source

Returns

string – The logout path.

Signature

public string getLogoutPath ( )

getResourceTrigger()

Returns the Resource Request trigger word based on the type of the current request. If it’s a front-end request, the resourceTrigger config setting value will be returned. Otherwise 'resources' will be returned.

View source

Returns

string – The Resource Request trigger word.

Signature

public string getResourceTrigger ( )

getUserSessionDuration()

Returns the configured user session duration in seconds, or null if there is none because user sessions should expire when the HTTP session expires. You can choose whether the rememberedUserSessionDuration or userSessionDuration config setting should be used with the $remembered param. If rememberedUserSessionDuration’s value is empty (disabling the feature) then userSessionDuration will be used regardless of $remembered.

View source

Arguments

  • $remembered (boolean) – Whether the rememberedUserSessionDuration config setting should be used if it’s set. Default is false.

Returns

integer, null – The user session duration in seconds, or null if user sessions should expire along with the HTTP session.

Signature

public integer, null getUserSessionDuration ( $remembered = false )

maxPowerCaptain()

Sets PHP’s memory limit to the maximum specified by the phpMaxMemoryLimit config setting, and gives the script an unlimited amount of time to execute.

View source

Returns

null

Signature

public null maxPowerCaptain ( )

omitScriptNameInUrls()

Returns whether generated URLs should omit “index.php”, taking the omitScriptNameInUrls config setting value into account. If the omitScriptNameInUrls config setting is set to true or false, then its value will be returned directly. Otherwise, omitScriptNameInUrls() will try to determine whether the server is set up to support index.php redirection. (See this help article for instructions.)

It does that by creating a dummy request to the site URL with the URI “/testScriptNameRedirect”. If the index.php redirect is in place, that request should be sent to Craft’s index.php file, which will detect that this is an index.php redirect-testing request, and simply return “success”. If anything besides “success” is returned (i.e. an Apache-styled 404 error), then Craft assumes the index.php redirect is not in fact in place.

Results of the redirect test request will be cached for the amount of time specified by the cacheDuration config setting.

View source

Returns

boolean – Whether generated URLs should omit “index.php”.

Signature

public boolean omitScriptNameInUrls ( )

parseEnvironmentString()

Parses a string for any environment variables. This method simply loops through all of the elements in the environmentVariables config setting’s value, and replaces any {tag}s in the string that have matching keys with their corresponding values.

View source

Arguments

  • $str (string) – The string that should be parsed for environment variables.

Returns

string – The parsed string.

Signature

public string parseEnvironmentString ( $str )

set()

Overrides the value of a config setting to a given value. By default, set() will update the config array that came from craft/config/general.php. See Craft’s documentation for a full list of config settings that Craft will check for within that file.

craft()->config->set('devMode', true);

If you want to set a config setting from a different config file (e.g. config/myplugin.php), you can specify its filename as a third argument.

craft()->config->set('myConfigSetting', 'foo', 'myplugin');

View source

Arguments

  • $item (string) – The name of the config setting.
  • $value (mixed) – The new value of the config setting.
  • $file (string) – The name of the config file (sans .php). Defaults to 'general'.

Returns

null

Signature

public null set ( $item, $value, $file = \Craft\ConfigFile::General )

usePathInfo()

Returns whether generated URLs should be formatted using PATH_INFO, taking the usePathInfo config setting value into account. This method will usually only be called in the event that {@link omitScriptNameInUrls()} returns false (so “index.php” should be included), and it determines what follows “index.php” in the URL.

If it returns true, a forward slash will be used, making “index.php” look like just another segment of the URL (e.g. http://example.com/index.php/some/path). Otherwise the Craft path will be included in the URL as a query string param named ‘p’ (e.g. http://example.com/index.php?p=some/path).

If the usePathInfo config setting is set to true or false, then its value will be returned directly. Otherwise, usePathInfo() will try to determine whether the server is set up to support PATH_INFO. (See http://craftcms.com/help/enable-path-info for instructions.)

It does that by creating a dummy request to the site URL with the URI “/index.php/testPathInfo”. If the server supports it, that request should be sent to Craft’s index.php file, which will detect that this is an PATH_INFO-testing request, and simply return “success”. If anything besides “success” is returned (i.e. an Apache-styled 404 error), then Craft assumes the server is not set up to support PATH_INFO.

Results of the PATH_INFO test request will be cached for the amount of time specified by the cacheDuration config setting.

View source

Returns

boolean – Whether generaletd URLs should be formatted using PATH_INFO.

Signature

public boolean usePathInfo ( )