mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-23 18:18:21 +08:00
Merge branch 'master' into docs-ja-2014-10-18
This commit is contained in:
@@ -15,12 +15,13 @@ use yii\helpers\Html;
|
||||
echo Html::encode('Test > test');
|
||||
```
|
||||
|
||||
> Note: To support [extending helper classes](#extending-helper-classes), Yii breaks each core helper class
|
||||
> Note: To support [customizing helper classes](#customizing-helper-classes), Yii breaks each core helper class
|
||||
into two classes: a base class (e.g. `BaseArrayHelper`) and a concrete class (e.g. `ArrayHelper`).
|
||||
When you use a helper, you should only use the concrete version and never use the base class.
|
||||
|
||||
|
||||
## Core Helper Classes
|
||||
Core Helper Classes
|
||||
-------------------
|
||||
|
||||
The following core helper classes are provided in the Yii releases:
|
||||
|
||||
@@ -39,19 +40,21 @@ The following core helper classes are provided in the Yii releases:
|
||||
- VarDumper
|
||||
|
||||
|
||||
## Extending Helper Classes
|
||||
Customizing Helper Classes <a name="customizing-helper-classes"></a>
|
||||
--------------------------
|
||||
|
||||
To custom a core helper class (e.g. `yii\helpers\ArrayHelper`), you should extend from its corresponding base class
|
||||
(e.g. `yii\helpers\BaseArrayHelper`) and name your class the same as the corresponding concrete class
|
||||
(e.g. `yii\helpers\ArrayHelper`), including its namespace.
|
||||
To customize a core helper class (e.g. [[yii\helpers\ArrayHelper]]), you should create a new class extending
|
||||
from the helpers corresponding base class (e.g. [[yii\helpers\BaseArrayHelper]]) and name your class the same
|
||||
as the corresponding concrete class (e.g. [[yii\helpers\ArrayHelper]]), including its namespace. This class
|
||||
will then be set up to replace the original implementation of the framework.
|
||||
|
||||
The following example shows how to customize the [[yii\helpers\ArrayHelper::merge()|merge()]] method of the
|
||||
[[yii\helpers\ArrayHelper]] class:
|
||||
|
||||
```php
|
||||
namespace yii\helpers;
|
||||
<?php
|
||||
|
||||
use yii\helpers\BaseArrayHelper;
|
||||
namespace yii\helpers;
|
||||
|
||||
class ArrayHelper extends BaseArrayHelper
|
||||
{
|
||||
@@ -62,14 +65,16 @@ class ArrayHelper extends BaseArrayHelper
|
||||
}
|
||||
```
|
||||
|
||||
Save your class in a file named `ArrayHelper.php`. The file can be in any directory, such as `@app/components`.
|
||||
Save your class in a file named `ArrayHelper.php`. The file can be in any directory, for example `@app/components`.
|
||||
|
||||
Next, in your application's [entry script](structure-entry-scripts.md), add the following line of code
|
||||
after including the `yii.php` file:
|
||||
after including the `yii.php` file to tell the [Yii class autoloader](concept-autoloading.md) to load your custom
|
||||
class instead of the original helper class from the framework:
|
||||
|
||||
```php
|
||||
Yii::$classMap['yii\helpers\ArrayHelper'] = 'path/to/ArrayHelper.php';
|
||||
Yii::$classMap['yii\helpers\ArrayHelper'] = '@app/components/ArrayHelper.php';
|
||||
```
|
||||
|
||||
The above line instructs the [Yii class autoloader](concept-autoloading.md) to load your version of the helper
|
||||
class, instead of the one included in the Yii releases.
|
||||
Note that customizing of helper classes is only useful if you want to change the behavior of an existing function
|
||||
of the helpers. If you want to add additional functions to use in your application you may better create a separate
|
||||
helper for that.
|
||||
|
||||
Reference in New Issue
Block a user