Yii2 Helpers Guide

By Prem Tiwari in Yii framework. Updated on


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.

yii2-helper-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::buttonInput()
yii\helpers\Html::submitInput()
yii\helpers\Html::resetInput()
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()

Sharing is caring!


Prem Tiwari

An engineer by profession and a passionate blogger by heart. Founder of FreeWebMentor.com (A Programming blog for beginners), Tech Speaker at various forums. A part from this he is an open source enthusiast, WordPress Lover, Blogger, SEO, and Growth Strategic.

You might also like:

Tags: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.