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
the base URL for the currently running Web application.
Yii 2.0 expands the usage of path aliases to both file/directory paths and URLs. An alias
must start with an `@` symbol so that it can be differentiated from file/directory paths and URLs.
For example, the alias `@yii` refers to the Yii installation directory while `@web` contains the base URL for the currently running web application. Path aliases are supported in most places in the Yii core code. For example, `FileCache::cachePath` can accept both a path alias and a normal directory path.
Path aliases are also closely related to class namespaces. It is recommended that a path
alias should be defined for each root namespace so that Yii's class autoloader can be used without
any further configuration. For example, because `@yii` refers to the Yii installation directory,
a class like `yii\web\Request` can be autoloaded by Yii. If you use a third party library
such as Zend Framework, you may define a path alias `@Zend` which refers to its installation
directory and Yii will be able to autoload any class in this library.
Defining Aliases
----------------
The following aliases are predefined by the core framework:
You can call [[Yii::setAlias()]] to define an alias for a given file path or URL. For example,
-`@yii` - framework directory.
-`@app` - base path of currently running application.
-`@runtime` - runtime directory.
-`@vendor` - Composer vendor directory.
-`@webroot` - web root directory of currently running web application.
-`@web` - base URL of currently running web application.
```php
// an alias of file path
Yii::setAlias('@foo','/path/to/foo');
// an alias of 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.
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,
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):
```php
Yii::setAlias('@foobar','@foo/bar');
```
Root aliases are usually defined during the [bootstrapping](runtime-bootstrapping.md) stage.
For example, you may call [[Yii::setAlias()]] in the [entry script](structure-entry-scripts.md).
For convenience, [Application](structure-applications.md) provides writable property named `aliases`
that you can configure in the application [configuration](basic-configs.md), like the following,
```php
return[
// ...
'aliases'=>[
'@foo'=>'/path/to/foo',
'@bar'=>'http://www.example.com',
],
];
```
Resolving Aliases
-----------------
You can call [[Yii::getAlias()]] to resolve a root alias into the file path or URL it is representing.
The same method can also resolve a derived alias into the corresponding file path or URL. For example,