BaseYii
API reference › yii › BaseYii
1 subclass
BaseYii is the core helper class for the Yii framework.
Do not use BaseYii directly. Instead, use its child class Yii which you can replace to
customize methods of BaseYii.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$aliases static | array | Registered path aliases Default: ['@yii' => __DIR__]. | BaseYii |
$app static | yii\|yii\<yii\ | The application instance Default: null. | BaseYii |
$container static | yii\ | The dependency injection (DI) container used by createObject(). Default: null. | BaseYii |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
beginProfile() static | Marks the beginning of a code block for profiling. | BaseYii |
configure() static | Configures an object with the initial property values. | BaseYii |
createObject() static | Creates a new object using the given configuration. | BaseYii |
debug() static | Logs a debug message. | BaseYii |
endProfile() static | Marks the end of a code block for profiling. | BaseYii |
error() static | Logs an error message. | BaseYii |
getAlias() static | Translates a path alias into an actual path. | BaseYii |
getLogger() static | BaseYii | |
getObjectVars() static | Returns the public member variables of an object. | BaseYii |
getRootAlias() static | Returns the root alias part of a given alias. | BaseYii |
getVersion() static | Returns a string representing the current version of the Yii framework. | BaseYii |
info() static | Logs an informative message. | BaseYii |
setAlias() static | Registers a path alias. | BaseYii |
setLogger() static | Sets the logger object. | BaseYii |
t() static | Translates a message to the specified language. | BaseYii |
warning() static | Logs a warning message. | BaseYii |
Property details
Section titled “Property details”$container
Section titled “$container ”public static yii\di\Container $container = nullThe dependency injection (DI) container used by createObject().
You may use Container::set() to set up the needed dependencies of classes and
their initial property values.
Method details
Section titled “Method details”beginProfile()
Section titled “beginProfile() ”public static function beginProfile(string $token, string $category = 'application'): mixedMarks the beginning of a code block for profiling.
This has to be matched with a call to endProfile() with the same category name.
The begin- and end- calls must also be properly nested. For example,
\Yii::beginProfile('block1');// some code to be profiled \Yii::beginProfile('block2'); // some other code to be profiled \Yii::endProfile('block2');\Yii::endProfile('block1');Parameters
| Parameter | Type | Description |
|---|---|---|
$token | string | Token for the code block |
$category | string | The category of this log message |
Returns
mixed
See also
configure()
Section titled “configure() ”public static function configure(object $object, array $properties): objectConfigures an object with the initial property values.
Parameters
| Parameter | Type | Description |
|---|---|---|
$object | object | The object to be configured |
$properties | array | The property initial values given in terms of name-value pairs. |
Returns
object: The object itself
createObject()
Section titled “createObject() ”public static function createObject(class-string<yii\T>|array{class?: class-string<yii\T>, __class?: class-string<yii\T>}|callable(): yii\T $type, array $params = []): yii\TCreates a new object using the given configuration.
You may view this method as an enhanced version of the new operator.
The method supports creating an object based on a class name, a configuration array or
an anonymous function.
Below are some usage examples:
// create an object using a class name$object = Yii::createObject('yii\db\Connection');
// create an object using a configuration array$object = Yii::createObject([ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', 'username' => 'root', 'password' => '', 'charset' => 'utf8',]);
// create an object with two constructor parameters$object = \Yii::createObject('MyClass', [$param1, $param2]);Using dependency injection container, this method can also identify dependent objects, instantiate them and inject them into the newly created object.
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | class-string<yii\ | The object type. This can be specified in one of the following forms: - a string: representing the class name of the object to be created - a configuration array: the array must contain a class element which is treated as the object class, and the rest of the name-value pairs will be used to initialize the corresponding object properties - a PHP callable: either an anonymous function or an array representing a class method ([$class or $object,). The callable should return a new instance of the object being created. |
$params | array | The constructor parameters |
Returns
yii\T: The created object
Throws
yii\base\InvalidConfigException: if the configuration is invalid.
See also
debug()
Section titled “debug() ”public static function debug(string|array $message, string $category = 'application'): mixedLogs a debug message.
Trace messages are logged mainly for development purposes to see the execution workflow of some code. This method will only log a message when the application is in debug mode.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | string| | The message to be logged. This can be a simple string or a more complex data structure, such as an array. |
$category | string | The category of the message. |
Returns
mixed
Since
2.0.14
endProfile()
Section titled “endProfile() ”public static function endProfile(string $token, string $category = 'application'): mixedMarks the end of a code block for profiling.
This has to be matched with a previous call to beginProfile() with the same category name.
Parameters
| Parameter | Type | Description |
|---|---|---|
$token | string | Token for the code block |
$category | string | The category of this log message |
Returns
mixed
See also
error()
Section titled “error() ”public static function error(string|array|Throwable $message, string $category = 'application'): mixedLogs an error message.
An error message is typically logged when an unrecoverable error occurs during the execution of an application.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | string|Throwable | The message to be logged. This can be a simple string or a more complex data structure, such as an array. |
$category | string | The category of the message. |
Returns
mixed
getAlias()
Section titled “getAlias() ”public static function getAlias(string $alias, bool $throwException = true): ($throwException is true ? string : string|false)Translates a path alias into an actual path.
The translation is done according to the following procedure:
- If the given alias does not start with ’@’, it is returned back without change;
- Otherwise, look for the longest registered alias that matches the beginning part of the given alias. If it exists, replace the matching part of the given alias with the corresponding registered path.
- Throw an exception or return false, depending on the
$throwExceptionparameter.
For example, by default ‘@yii’ is registered as the alias to the Yii framework directory, say ‘/path/to/yii’. The alias ‘@yii/web’ would then be translated into ‘/path/to/yii/web’.
If you have registered two aliases ‘@foo’ and ‘@foo/bar’. Then translating ‘@foo/bar/config’ would replace the part ‘@foo/bar’ (instead of ‘@foo’) with the corresponding registered path. This is because the longest alias takes precedence.
However, if the alias to be translated is ‘@foo/barbar/config’, then ‘@foo’ will be replaced instead of ‘@foo/bar’, because ’/’ serves as the boundary character.
Note, this method does not check if the returned path exists or not.
See the guide article on aliases for more information.
Parameters
| Parameter | Type | Description |
|---|---|---|
$alias | string | The alias to be translated. |
$throwException | bool | Whether to throw an exception if the given alias is invalid. If this is false and an invalid alias is given, false will be returned by this method. |
Returns
($throwException is true ? string : string|false): The path corresponding to the alias, false if the root alias is not previously registered.
Throws
yii\base\InvalidArgumentException: if the alias is invalid while $throwException is true.
See also
getLogger()
Section titled “getLogger() ”public static function getLogger(): yii\log\LoggerReturns
yii\log\Logger: Message logger
getObjectVars()
Section titled “getObjectVars() ”public static function getObjectVars(object $object): arrayReturns the public member variables of an object.
This method is provided such that we can get the public member variables of an object. It is different from “get_object_vars()” because the latter will return private and protected variables if it is called within the object itself.
Parameters
| Parameter | Type | Description |
|---|---|---|
$object | object | The object to be handled |
Returns
array: The public member variables of the object
getRootAlias()
Section titled “getRootAlias() ”public static function getRootAlias(string $alias): string|falseReturns the root alias part of a given alias.
A root alias is an alias that has been registered via setAlias() previously.
If a given alias matches multiple root aliases, the longest one will be returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
$alias | string | The alias |
Returns
string|false: The root alias, or false if no root alias is found
getVersion()
Section titled “getVersion() ”public static function getVersion(): stringReturns a string representing the current version of the Yii framework.
Returns
string: The version of Yii framework
info()
Section titled “info() ”public static function info(string|array|Throwable $message, string $category = 'application'): mixedLogs an informative message.
An informative message is typically logged by an application to keep record of something important (e.g. an administrator logs in).
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | string|Throwable | The message to be logged. This can be a simple string or a more complex data structure, such as an array. |
$category | string | The category of the message. |
Returns
mixed
setAlias()
Section titled “setAlias() ”public static function setAlias(string $alias, string|null $path): mixedRegisters a path alias.
A path alias is a short name representing a long path (a file path, a URL, etc.) For example, we use ‘@yii’ as the alias of the path to the Yii framework directory.
A path alias must start with the character ’@’ so that it can be easily differentiated from non-alias paths.
Note that this method does not check if the given path exists or not. All it does is to associate the alias with the path.
Any trailing ’/’ and ” characters in the given path will be trimmed.
See the guide article on aliases for more information.
Parameters
| Parameter | Type | Description |
|---|---|---|
$alias | string | The alias name (e.g. “@yii”). It must start with a ’@’ character. It may contain the forward-slash ’/’ which serves as a boundary character when performing alias translation by getAlias(). |
$path | string| | The path corresponding to the alias. If this is null, the alias will be removed. Trailing ’/’ and ” characters will be trimmed. This can be - a directory or a file path (e.g. /tmp, /tmp/main.txt) - a URL (e.g. https://www.yiiframework.com) - a path alias (e.g. @yii/base). In this case, the path alias will be converted into the actual path first by calling getAlias(). |
Returns
mixed
Throws
yii\base\InvalidArgumentException: if $path is an invalid alias.
See also
setLogger()
Section titled “setLogger() ”public static function setLogger(yii\log\Logger|null $logger): mixedSets the logger object.
public static function t(string $category, string $message, array $params = [], string|null $language = null): stringTranslates a message to the specified language.
This is a shortcut method of I18N::translate().
The translation will be conducted according to the message category and the target language will be used.
You can add parameters to a translation message that will be substituted with the corresponding value after translation. The format for this is to use curly brackets around the parameter name as you can see in the following example:
$username = 'Alexander';echo \Yii::t('app', 'Hello, {username}!', ['username' => $username]);Further formatting of message parameters is supported using the PHP intl extensions
message formatter. See I18N::translate() for more details.
Parameters
| Parameter | Type | Description |
|---|---|---|
$category | string | The message category. |
$message | string | The message to be translated. |
$params | array | The parameters that will be used to replace the corresponding placeholders in the message. |
$language | string| | The language code (e.g. en-US, en). If this is null, the current application language will be used. |
Returns
string: The translated message.
warning()
Section titled “warning() ”public static function warning(string|array|Throwable $message, string $category = 'application'): mixedLogs a warning message.
A warning message is typically logged when an error occurs while the execution can still continue.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | string|Throwable | The message to be logged. This can be a simple string or a more complex data structure, such as an array. |
$category | string | The category of the message. |
Returns
mixed