Skip to content

App

Type
Class
Namespace
craft\helpers
Inherits
craft\helpers\App
Since
3.0.0

App helper.

View source

Public Methods

MethodDescription
assetManagerConfig()Returns the assetManager component config for web requests.
backtrace()Returns the backtrace as a string (omitting the final frame where this method was called).
cacheConfig()Returns the cache component config.
checkForValidIconv()Returns whether the server has a valid version of the iconv extension installed.
cliOption()Returns a CLI command option from argv, or null if it wasn’t passed.
configure()Configures an object with property values.
createFormattingLocale()Creates a locale object that should be used for date and number formatting.
dbConfig()Returns the db component config.
dbMutexConfig()Returns a database-based mutex driver config.
devMode()Returns whether Dev Mode is enabled.
editionHandle()Returns the handle of the given Craft edition.
editionIdByHandle()Returns the ID of a Craft edition by its handle.
editionName()Returns the name of the given Craft edition.
editions()Returns an array of all known Craft editions’ IDs.
env()Returns an environment-specific value.
envConfig()Returns a config array for a given class, based on any environment variables or PHP constants named based on its public properties.
extensionVersion()Returns a PHP extension version, without the distribution info.
humanizeClass()Returns a humanized class name.
isEphemeral()Returns whether Craft is running on an environment with ephemeral storage.
isPathAllowed()Returns whether the given path is within PHP’s open_basedir setting.
isStreamLog()Returns whether Craft is logging to stdout/stderr.
isTty()Returns whether Craft is being run from a TTY terminal.
isValidEdition()Returns whether an edition is valid.
isWindows()Returns whether Craft is running on a Windows environment
licenseKey()
mailSettings()Returns the system email settings.
mailerConfig()Returns the mailer component config.
maxPowerCaptain()Sets PHP’s memory limit to the maximum specified by the config5:phpMaxMemoryLimit config setting, and gives the script an unlimited amount of time to execute.
mutexConfig()Returns a file-based mutex driver config.
normalizePhpPaths()Normalizes a PHP path setting to an array of paths
normalizeValue()Normalizes an environment variable/constant name/CLI command option.
normalizeVersion()Removes distribution info from a version string, and returns the highest version number found in the remainder.
parseBooleanEnv()Checks if a string references an environment variable ($VARIABLE_NAME) and returns the referenced boolean value, or null if a boolean value can’t be determined.
parseEnv()Checks if a string references an environment variable ($VARIABLE_NAME) and/or an alias (@aliasName), and returns the referenced value.
phpConfigValueAsBool()Retrieves a bool PHP config setting and normalizes it to an actual bool.
phpConfigValueAsPaths()Retrieves a file path PHP config setting and normalizes it to an array of paths.
phpConfigValueInBytes()Retrieves a disk size PHP config setting and normalizes it into bytes.
phpExecutable()Returns the path to a PHP executable which should be used by sub processes.
phpSizeToBytes()Normalizes a PHP file size into bytes.
phpVersion()Returns the PHP version, without the distribution info.
projectConfigConfig()Returns the projectConfig component config.
sessionConfig()Returns the session component config for web requests.
silence()Calls the given closure with all error reporting silenced, and returns its response.
supportsIdn()Returns whether the server supports IDNA ASCII strings.
testIniSet()Tests whether ini_set() works.
userConfig()Returns the user component config for web requests.
viewConfig()Returns the view component config.
webRequestConfig()Returns the request component config for web requests.
webResponseConfig()Returns the response component config for web requests.

assetManagerConfig()

Since
3.0.18

Returns the assetManager component config for web requests.

View source

Returns

array

backtrace()

Since
3.0.13

Returns the backtrace as a string (omitting the final frame where this method was called).

View source

Arguments

  • $limit (integer) – The max number of stack frames to be included (0 means no limit)

Returns

string

cacheConfig()

Since
3.0.18

Returns the cache component config.

View source

Returns

array

checkForValidIconv()

Returns whether the server has a valid version of the iconv extension installed.

View source

Returns

boolean

cliOption()

Since
4.0.0

Returns a CLI command option from argv, or null if it wasn’t passed.

Supported option syntaxes are:

  • name=value
  • name value
  • name (implies true)

name must begin with -- or -. Other values will be rejected.

If the value is numeric, a float or int will be returned.

If the value is true or false, a boolean will be returned.

If the option has no value (either because the following item begins with - or it’s the last item), true will be returned.

View source

Arguments

  • $name (string) – The option name, beginning with -- or -
  • $unset (boolean) – Whether the option should be removed from argv if found

Returns

string, float, integer, true, null

configure()

Since
5.3.0

Configures an object with property values.

This is identical to \BaseYii::configure(), except this class is safe to be called during application bootstrap, whereas \BaseYii is not.

View source

Arguments

  • $object (object) – The object to be configured
  • $properties (array) – The property initial values given in terms of name-value pairs.

createFormattingLocale()

Since
3.6.0

Creates a locale object that should be used for date and number formatting.

View source

Returns

craft\i18n\Locale

dbConfig()

Since
3.0.18

Returns the db component config.

View source

Arguments

Returns

array

dbMutexConfig()

Since
4.6.0

Returns a database-based mutex driver config.

View source

Returns

array

devMode()

Since
4.0.0

Returns whether Dev Mode is enabled.

View source

Returns

boolean

editionHandle()

DEPRECATED

Deprecated in 5.0.0. CmsEdition::handle() should be used instead.

Since
3.1.0

Returns the handle of the given Craft edition.

View source

Arguments

  • $edition (integer) – An edition’s ID.

Returns

string – The edition’s handle.

Throws

editionIdByHandle()

DEPRECATED

Deprecated in 5.0.0. CmsEdition::fromHandle() should be used instead.

Since
3.1.0

Returns the ID of a Craft edition by its handle.

View source

Arguments

  • $handle (string) – An edition’s handle

Returns

integer – The edition’s ID

Throws

editionName()

DEPRECATED

Deprecated in 5.0.0. CmsEdition::name should be used instead.

Returns the name of the given Craft edition.

View source

Arguments

  • $edition (integer) – An edition’s ID.

Returns

string – The edition’s name.

Throws

editions()

DEPRECATED

Deprecated in 5.0.0. CmsEdition::cases() should be used instead.

Returns an array of all known Craft editions’ IDs.

View source

Returns

integer[] – All the known Craft editions’ IDs.

env()

Since
3.4.18

Returns an environment-specific value.

Values will be looked for in the following places:

  1. “Secret” values returned by a PHP file identified by a CRAFT_SECRETS_PATH environment variable
  2. Environment variables stored in $_SERVER
  3. Environment variables returned by getenv()
  4. PHP constants

If the value cannot be found, null will be returned.

View source

Arguments

  • $name (string) – The name to search for.

Returns

mixed – The value, or null if not found.

Throws

envConfig()

Since
4.0.0

Returns a config array for a given class, based on any environment variables or PHP constants named based on its public properties.

Environment variable/PHP constant names must be capitalized, SNAKE_CASED versions of the object’s property names, possibly with a given prefix.

For example, if an object has a fooBar property, and X/X_ is passed as the prefix, the resulting array may contain a fooBar key set to an X_FOO_BAR environment variable value, if it exists.

View source

Arguments

  • $class (class-string) – The class name
  • $envPrefix (string, null) – The environment variable name prefix

Returns

array

extensionVersion()

Returns a PHP extension version, without the distribution info.

View source

Arguments

  • $name (string) – The extension name

Returns

string

humanizeClass()

Returns a humanized class name.

View source

Arguments

  • $class (class-string)

Returns

string

isEphemeral()

Since
3.4.0

Returns whether Craft is running on an environment with ephemeral storage.

View source

Returns

boolean

isPathAllowed()

Since
3.7.34

Returns whether the given path is within PHP’s open_basedir setting.

View source

Arguments

Returns

boolean

isStreamLog()

Since
4.0.0

Returns whether Craft is logging to stdout/stderr.

View source

Returns

boolean

isTty()

Since
5.4.8

Returns whether Craft is being run from a TTY terminal.

This is copied verbatim from Composer\Util\Platform::isTty(). Full credit to Nils Adermann and Jordi Boggiano.

View source

Arguments

  • $fd (resource, null) – Open file descriptor or null. Defaults to STDOUT.

isValidEdition()

DEPRECATED

Deprecated in 5.0.0. CmsEdition::tryFrom() should be used instead.

Returns whether an edition is valid.

View source

Arguments

  • $edition (mixed) – An edition’s ID (or is it?)

Returns

boolean – Whether $edition is a valid edition ID.

isWindows()

Since
5.0.0

Returns whether Craft is running on a Windows environment

View source

licenseKey()

View source

Returns

string, null

mailSettings()

Since
3.1.0

Returns the system email settings.

View source

Returns

craft\models\MailSettings

mailerConfig()

Since
3.0.18

Returns the mailer component config.

View source

Arguments

Returns

array

maxPowerCaptain()

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

View source

mutexConfig()

DEPRECATED

Deprecated in 4.6.0

Since
3.0.18

Returns a file-based mutex driver config.

TIP

If you were calling this to override the yii\mutex\FileMutex::$isWindows property, note that overriding the mutex component may no longer be necessary, as Craft no longer uses a mutex when Dev Mode is enabled.

View source

Returns

array

normalizePhpPaths()

Since
3.7.34

Normalizes a PHP path setting to an array of paths

View source

Arguments

  • $value (string) – The PHP path setting value

Returns

string[] – The normalized paths

normalizeValue()

Since
4.0.0

Normalizes an environment variable/constant name/CLI command option.

It converts the following:

  • 'true'true
  • 'false'false
  • Numeric string → integer or float

View source

Arguments

  • $value (mixed)

Returns

mixed

normalizeVersion()

Removes distribution info from a version string, and returns the highest version number found in the remainder.

View source

Arguments

Returns

string

parseBooleanEnv()

Since
3.7.29

Checks if a string references an environment variable ($VARIABLE_NAME) and returns the referenced boolean value, or null if a boolean value can’t be determined.

View source

Arguments

  • $value (mixed)

Returns

boolean, null


Example

php
$status = App::parseBooleanEnv('$SYSTEM_STATUS') ?? false;

parseEnv()

Since
3.7.29

Checks if a string references an environment variable ($VARIABLE_NAME) and/or an alias (@aliasName), and returns the referenced value.

If the string references an environment variable with a value of true or false, a boolean value will be returned.

If the string references an environment variable that’s not defined, null will be returned.

View source

Arguments

Returns

string, boolean, null – The parsed value, or the original value if it didn’t reference an environment variable and/or alias.


Example

php
$value1 = App::parseEnv('$SMTP_PASSWORD');
$value2 = App::parseEnv('@webroot');

phpConfigValueAsBool()

Retrieves a bool PHP config setting and normalizes it to an actual bool.

View source

Arguments

  • $var (string) – The PHP config setting to retrieve.

Returns

boolean – Whether it is set to the php.ini equivalent of true.

phpConfigValueAsPaths()

Since
3.7.34

Retrieves a file path PHP config setting and normalizes it to an array of paths.

View source

Arguments

  • $var (string) – The PHP config setting to retrieve

Returns

string[] – The normalized paths

phpConfigValueInBytes()

Since
3.0.38

Retrieves a disk size PHP config setting and normalizes it into bytes.

View source

Arguments

  • $var (string) – The PHP config setting to retrieve.

Returns

integer, float – The value normalized into bytes.

phpExecutable()

Since
4.5.6

Returns the path to a PHP executable which should be used by sub processes.

View source

Returns

string, null – The PHP executable path, or null if it can’t be determined.

phpSizeToBytes()

Since
3.6.0

Normalizes a PHP file size into bytes.

View source

Arguments

  • $value (string) – The file size expressed in PHP config value notation

Returns

integer, float – The value normalized into bytes.

phpVersion()

Returns the PHP version, without the distribution info.

View source

Returns

string

projectConfigConfig()

Returns the projectConfig component config.

View source

sessionConfig()

Since
3.0.18

Returns the session component config for web requests.

View source

Returns

array

silence()

Since
5.0.0

Calls the given closure with all error reporting silenced, and returns its response.

View source

Arguments

  • $callable (Closure, string)
  • $mask (integer, null) – Error levels to suppress, default value NULL indicates all warnings and below.

Returns

mixed

supportsIdn()

Since
3.7.9

Returns whether the server supports IDNA ASCII strings.

View source

Returns

boolean

testIniSet()

Since
3.0.40

Tests whether ini_set() works.

View source

Returns

boolean

userConfig()

Since
3.0.18

Returns the user component config for web requests.

View source

Returns

array

viewConfig()

Since
3.0.18

Returns the view component config.

View source

Returns

array

webRequestConfig()

Since
3.0.18

Returns the request component config for web requests.

View source

Returns

array

webResponseConfig()

Since
3.3.0

Returns the response component config for web requests.

View source

Returns

array