Aliases are used to represent file paths or URLs to avoid hard-coding absolute paths or URLs in your code.
An alias must start with a `@` character so that it can be differentiated from file paths and URLs.
For example, the alias `@yii` represents the installation path of the Yii framework, while `@web` represents
Aliases are used to represent file paths or URLs so that you don't have to hard-code absolute paths or URLs in your project. An alias must start with the `@` character to be differentiated from normal file paths and URLs. Yii has many pre-defined aliases already available.
For example, the alias `@yii` represents the installation path of the Yii framework; `@web` represents
the base URL for the currently running Web application.
Defining Aliases <a name="defining-aliases"></a>
----------------
You can call [[Yii::setAlias()]] to define an alias for a given file path or URL. For example,
You can define an alias for a file path or URL by calling [[Yii::setAlias()]]:
```php
// an alias of file path
// an alias of a file path
Yii::setAlias('@foo','/path/to/foo');
// an alias of URL
// an alias of a URL
Yii::setAlias('@bar','http://www.example.com');
```
> Note: A file path or URL being aliased may NOT necessarily refer to an existing file or resource.
> Note: The file path or URL being aliased may *not* necessarily refer to an existing file or resource.
Given an alias, you may derive a new alias (without the need of calling [[Yii::setAlias()]]) by appending
a slash `/` followed with one or several path segments. We call the aliases defined via [[Yii::setAlias()]]
*root aliases*, while the aliases derived from them*derived aliases*. For example, `@foo` is a root alias,
Given a defined alias, you may derive a new alias (without the need of calling [[Yii::setAlias()]]) by appending
a slash `/` followed with one or more path segments. The aliases defined via [[Yii::setAlias()]] becomes the
*root alias*, while aliases derived from it are*derived aliases*. For example, `@foo` is a root alias,
while `@foo/bar/file.php` is a derived alias.
You can define an alias using another alias (either root alias or derived alias is fine):
You can define an alias using another alias (either root or derived):
Yii predefines a set of aliases to ease the need of referencing commonly used file paths and URLs.
The following is the list of the predefined aliases:
Yii predefines a set of aliases to easily reference commonly used file paths and URLs:
-`@yii`: the directory where the `BaseYii.php` file is located (also called the framework directory).
-`@app`: the [[yii\base\Application::basePath|base path]] of the currently running application.
-`@runtime`: the [[yii\base\Application::runtimePath|runtime path]] of the currently running application.
-`@vendor`: the [[yii\base\Application::vendorPath|Composer vendor directory]].
-`@webroot`: the Web root directory of the currently running Web application.
-`@web`: the base URL of the currently running Web application.
-`@yii`, the directory where the `BaseYii.php` file is located (also called the framework directory)
-`@app`, the [[yii\base\Application::basePath|base path]] of the currently running application
-`@runtime`, the [[yii\base\Application::runtimePath|runtime path]] of the currently running application
-`@vendor`, the [[yii\base\Application::vendorPath|Composer vendor directory]]
-`@webroot`, the Web root directory of the currently running Web application
-`@web`, the base URL of the currently running Web application
The `@yii` alias is defined when you include the `Yii.php` file in your [entry script](structure-entry-scripts.md),
while the rest of the aliases are defined in the application constructor when applying the application
The `@yii` alias is defined when you include the `Yii.php` file in your [entry script](structure-entry-scripts.md). The rest of the aliases are defined in the application constructor when applying the application