Comparing
#209 with
#210
Revision #210 was created by rackycz on Jul 8, 2020, 4:57:25 PM.
Formatter
« Previous (#209) Next (#211) »
Content
[...]
}
```
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:[...]