Creating helpers
There are a lot of built-in framework helpers such as StringHelper
in the yii\helpers
namespace. These contain sets of helpful static methods for manipulating strings, files, arrays, and other subjects.
In many cases, for additional behavior you can create a own helper and put any static function into one. For example, we implement the number helper in this recipe.
Getting ready
Create a new yii2-app-basic
application using the composer package manager as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
How to do it…
Create the
helpers
directory in your project and write theNumberHelper
class:<?php namespace app\helpers; class NumberHelper { public static function format($value, $decimal = 2) { return number_format($value, $decimal, '.', ','); } }
Add the
actionNumbers
method toSiteController
:<?php ... class SiteController extends Controller { … public function actionNumbers() { ...