helper-overview.md 2.63 KB
Newer Older
Qiang Xue committed
1 2 3
Helpers
=======

4
> Note: This section is under development.
Qiang Xue committed
5

Qiang Xue committed
6 7 8 9 10
Yii provides many classes that help simplify common coding tasks, such as string or array manipulations,
HTML code generation, and so on. These helper classes are organized under the `yii\helpers` namespace and
are all static classes (meaning they contain only static properties and methods and should not be instantiated).

You use a helper class by directly calling one of its static methods, like the following:
Qiang Xue committed
11 12

```php
Qiang Xue committed
13 14
use yii\helpers\Html;

Qiang Xue committed
15 16 17
echo Html::encode('Test > test');
```

18
> Note: To support [customizing helper classes](#customizing-helper-classes), Yii breaks each core helper class
Qiang Xue committed
19 20 21 22
  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.


23 24
Core Helper Classes
-------------------
Qiang Xue committed
25 26

The following core helper classes are provided in the Yii releases:
Qiang Xue committed
27

28
- [ArrayHelper](helper-array.md)
Qiang Xue committed
29 30 31 32 33 34 35 36 37 38 39 40
- Console
- FileHelper
- Html
- HtmlPurifier
- Image
- Inflector
- Json
- Markdown
- Security
- StringHelper
- Url
- VarDumper
Qiang Xue committed
41 42


43 44
Customizing Helper Classes <a name="customizing-helper-classes"></a>
--------------------------
Qiang Xue committed
45

46 47 48 49
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.
Qiang Xue committed
50 51 52 53 54

The following example shows how to customize the [[yii\helpers\ArrayHelper::merge()|merge()]] method of the
[[yii\helpers\ArrayHelper]] class:

```php
55
<?php
Qiang Xue committed
56

57
namespace yii\helpers;
Qiang Xue committed
58 59 60 61 62 63 64 65 66 67

class ArrayHelper extends BaseArrayHelper
{
    public static function merge($a, $b)
    {
        // your custom implementation
    }
}
```

68
Save your class in a file named `ArrayHelper.php`. The file can be in any directory, for example `@app/components`.
Qiang Xue committed
69 70

Next, in your application's [entry script](structure-entry-scripts.md), add the following line of code
71 72
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:
Qiang Xue committed
73 74

```php
75
Yii::$classMap['yii\helpers\ArrayHelper'] = '@app/components/ArrayHelper.php';
Qiang Xue committed
76 77
```

78 79 80
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.