Yii2 Helpers Guide

Hello friends hope you are doing good, today i am going to share Yii2 Helpers Guide that will help you in your next yii2 application coding tasks. Yii2 framework provides several helper classes that help you to simplify your common coding tasks like string / array manipulations, HTML code and much more. These helper classes are listed under the yii\helpers namespace and yii static classes.


Use Yii2 helper class by directly calling of static methods, use below code example to call helper classes in your custom codes.

Yii2 Core Helper Classes

Following is the some core helper classes:

  • ArrayHelper
  • Console
  • FileHelper
  • FormatConverter
  • Html
  • HtmlPurifier
  • Imagine (provided by yii2-imagine extension)
  • Inflector
  • Json
  • Markdown
  • StringHelper
  • Url
  • VarDumper

Generating Yii2 Tags

Want to generate the custom tags for your applcation, use below code snippets.

The above code will generate the output like following :

Forming Your CSS Classes and Styles

If you want to forming your css classes, you can use the following code:

Forming multiple CSS classes using the array:

Also use the array style for adding or removing css classes:sses:

Encoding and Decoding Your Content

In PHP this will done by using htmlspecialchars and htmlspecialchars_decode functions, But Yii2 provides simple methods.

Below is the lists of some yii input type shortcut methods :

yii\helpers\Html::textInput(), yii\helpers\Html::activeTextInput()
yii\helpers\Html::hiddenInput(), yii\helpers\Html::activeHiddenInput()
yii\helpers\Html::passwordInput() / yii\helpers\Html::activePasswordInput()
yii\helpers\Html::fileInput(), yii\helpers\Html::activeFileInput()
yii\helpers\Html::textarea(), yii\helpers\Html::activeTextarea()

If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.

Recommended Posts:

Article Tags: , , , ,