Revision #210 has been created by rackycz on Jul 8, 2020, 4:57:25 PM with the memo:
Formatter
« previous (#209) next (#211) »
Changes
Title
unchanged
Yii v2 snippet guide
Category
unchanged
Tutorials
Yii version
unchanged
2.0
Tags
unchanged
tutorial,beginner,yii2
Content
changed
[...]
}
```
As you can see in the snippet above, other controllers must contain row "use app\controllers\BaseController" + "extends BaseController"
.
..
**Formatting values based on your Locale **
---
Go to config\web.php and add following values:
```php
$config = [
// ..
'language' => 'cs-CZ',
// \Yii::$app->language:
// https://www.yiiframework.com/doc/api/2.0/yii-base-application#$language-detail
//..
'components' => [
'formatter' => [
//'locale' => 'cs_CZ',
// Only effective when the "PHP intl extension" is installed else "language" above is used:
// https://www.php.net/manual/en/book.intl.php
//'language' => 'cs-CZ',
// If not set, "locale" above will be used:
// https://www.yiiframework.com/doc/api/2.0/yii-i18n-formatter#$language-detail
// Following values might be usefull for your situation:
'booleanFormat' => ['Ne', 'Ano'],
'dateFormat' => 'yyyy-mm-dd', // or 'php:Y-m-d'
'datetimeFormat' => 'yyyy-mm-dd HH:mm:ss', // or 'php:Y-m-d H:i:s'
'decimalSeparator' => ',',
'defaultTimeZone' => 'Europe/Prague',
'thousandSeparator' => ' ',
'timeFormat' => 'php:H:i:s', // or HH:mm:ss
'currencyCode' => 'CZK',
],
```
In GridView and DetailView you can then use following and your settings from above will be used:
```php
'columns' => [
[
'attribute' => 'colName',
'label' => 'Value',
'format'=>['decimal',2]
],
[
'label' => 'Value',
'value'=> function ($model) { return \Yii::$app->formatter->asDecimal($model->myCol, 2) . ' EUR' ; } ],
]
// ...
]
```
PS: I do not use currency formatter as it always puts the currency name before the number. For example USD 123. But in my country we use format: 123 CZK.
More links on this topic:
- [yii\i18n\Formatter](https://www.yiiframework.com/doc/api/2.0/yii-i18n-formatter)
- [Examples](https://yii2-framework.readthedocs.io/en/stable/guide/output-formatter)
- [More examples](https://stackoverflow.com/questions/27078178/yii2-number-format)
**Simple access rights**
---
Every controller can allow different users/guests to use different actions. Method behaviors() can be used to do this. If you generate the controller using GII the method will be present and you will just add the "access-part" like this:[...]