Commit cc8ee8d7 by Larry Ullman

Edited aliases

parent 94326571
...@@ -16,6 +16,8 @@ Guidelines to go by when writing or editing any Yii documentation. ...@@ -16,6 +16,8 @@ Guidelines to go by when writing or editing any Yii documentation.
## Formatting ## Formatting
* Use *italics* for emphasis, never capitalization, bold, or underlines.
## Blocks ## Blocks
Blocks use the Markdown `> Type: `. There are four block types: Blocks use the Markdown `> Type: `. There are four block types:
......
Aliases Aliases
======= =======
Aliases are used to represent file paths or URLs to avoid hard-coding absolute paths or URLs in your code. 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.
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; `@web` represents
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. the base URL for the currently running Web application.
Defining Aliases <a name="defining-aliases"></a> 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 ```php
// an alias of file path // an alias of a file path
Yii::setAlias('@foo', '/path/to/foo'); Yii::setAlias('@foo', '/path/to/foo');
// an alias of URL // an alias of a URL
Yii::setAlias('@bar', 'http://www.example.com'); 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 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 several path segments. We call the aliases defined via [[Yii::setAlias()]] a slash `/` followed with one or more path segments. The aliases defined via [[Yii::setAlias()]] becomes the
*root aliases*, while the aliases derived from them *derived aliases*. For example, `@foo` is a root alias, *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. 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):
```php ```php
Yii::setAlias('@foobar', '@foo/bar'); Yii::setAlias('@foobar', '@foo/bar');
...@@ -36,7 +35,7 @@ Yii::setAlias('@foobar', '@foo/bar'); ...@@ -36,7 +35,7 @@ Yii::setAlias('@foobar', '@foo/bar');
Root aliases are usually defined during the [bootstrapping](runtime-bootstrapping.md) stage. 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 example, you may call [[Yii::setAlias()]] in the [entry script](structure-entry-scripts.md).
For convenience, [Application](structure-applications.md) provides a writable property named `aliases` For convenience, [Application](structure-applications.md) provides a writable property named `aliases`
that you can configure in the application [configuration](concept-configurations.md), like the following, that you can configure in the application [configuration](concept-configurations.md):
```php ```php
return [ return [
...@@ -52,8 +51,8 @@ return [ ...@@ -52,8 +51,8 @@ return [
Resolving Aliases <a name="resolving-aliases"></a> Resolving Aliases <a name="resolving-aliases"></a>
----------------- -----------------
You can call [[Yii::getAlias()]] to resolve a root alias into the file path or URL it is representing. You can call [[Yii::getAlias()]] to resolve a root alias into the file path or URL it represents.
The same method can also resolve a derived alias into the corresponding file path or URL. For example, The same method can also resolve a derived alias into the corresponding file path or URL:
```php ```php
echo Yii::getAlias('@foo'); // displays: /path/to/foo echo Yii::getAlias('@foo'); // displays: /path/to/foo
...@@ -69,7 +68,7 @@ path/URL in the derived alias. ...@@ -69,7 +68,7 @@ path/URL in the derived alias.
A root alias may also contain slash `/` characters. The [[Yii::getAlias()]] method A root alias may also contain slash `/` characters. The [[Yii::getAlias()]] method
is intelligent enough to tell which part of an alias is a root alias and thus correctly determines is intelligent enough to tell which part of an alias is a root alias and thus correctly determines
the corresponding file path or URL. For example, the corresponding file path or URL:
```php ```php
Yii::setAlias('@foo', '/path/to/foo'); Yii::setAlias('@foo', '/path/to/foo');
...@@ -84,8 +83,8 @@ If `@foo/bar` is not defined as a root alias, the last statement would display ` ...@@ -84,8 +83,8 @@ If `@foo/bar` is not defined as a root alias, the last statement would display `
Using Aliases <a name="using-aliases"></a> Using Aliases <a name="using-aliases"></a>
------------- -------------
Aliases are recognized in many places in Yii without the need of calling [[Yii::getAlias()]] to convert Aliases are recognized in many places in Yii without needing to call [[Yii::getAlias()]] to convert
them into paths/URLs. For example, [[yii\caching\FileCache::cachePath]] can accept both a file path them into paths or URLs. For example, [[yii\caching\FileCache::cachePath]] can accept both a file path
and an alias representing a file path, thanks to the `@` prefix which allows it to differentiate a file path and an alias representing a file path, thanks to the `@` prefix which allows it to differentiate a file path
from an alias. from an alias.
...@@ -103,18 +102,16 @@ Please pay attention to the API documentation to see if a property or method par ...@@ -103,18 +102,16 @@ Please pay attention to the API documentation to see if a property or method par
Predefined Aliases <a name="predefined-aliases"></a> Predefined Aliases <a name="predefined-aliases"></a>
------------------ ------------------
Yii predefines a set of aliases to ease the need of referencing commonly used file paths and URLs. Yii predefines a set of aliases to easily reference commonly used file paths and URLs:
The following is the list of the predefined aliases:
- `@yii`: the directory where the `BaseYii.php` file is located (also called the framework directory). - `@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. - `@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. - `@runtime`, the [[yii\base\Application::runtimePath|runtime path]] of the currently running application
- `@vendor`: the [[yii\base\Application::vendorPath|Composer vendor directory]]. - `@vendor`, the [[yii\base\Application::vendorPath|Composer vendor directory]]
- `@webroot`: the Web root directory of the currently running Web application. - `@webroot`, the Web root directory of the currently running Web application
- `@web`: the base URL 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), 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
while the rest of the aliases are defined in the application constructor when applying the application
[configuration](concept-configurations.md). [configuration](concept-configurations.md).
...@@ -122,9 +119,9 @@ Extension Aliases <a name="extension-aliases"></a> ...@@ -122,9 +119,9 @@ Extension Aliases <a name="extension-aliases"></a>
----------------- -----------------
An alias is automatically defined for each [extension](structure-extensions.md) that is installed via Composer. An alias is automatically defined for each [extension](structure-extensions.md) that is installed via Composer.
The alias is named after the root namespace of the extension as declared in its `composer.json` file, and it Each alias is named after the root namespace of the extension as declared in its `composer.json` file, and each alias
represents the root directory of the package. For example, if you install the `yiisoft/yii2-jui` extension, represents the root directory of the package. For example, if you install the `yiisoft/yii2-jui` extension,
you will automatically have the alias `@yii/jui` defined during the [bootstrapping](runtime-bootstrapping.md) stage: you will automatically have the alias `@yii/jui` defined during the [bootstrapping](runtime-bootstrapping.md) stage, equivalent to:
```php ```php
Yii::setAlias('@yii/jui', 'VendorPath/yiisoft/yii2-jui'); Yii::setAlias('@yii/jui', 'VendorPath/yiisoft/yii2-jui');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment