How to transfer actioncolumn?

There are actioncolumn in yii2, reassign these buttons, but given that the controllers abound, and not to repeat code from the mapper how to move them into a separate component? The code itself

['class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {delete}',
'buttons' => [
 'view' => function ($url, $model, $key) {
 return Html::a('<span class="fa fa-eye"></span>', $url, [
 'title' => Yii::t('backend', 'View'),
 'class' => 'btn btn-success btn-sm',
]);
},
 'update' => function ($url, $model, $key) {
 return Html::a('<span class="fa fa-pencil-square-o"></span>', $url, [
 'title' => Yii::t('backend', 'Update'),
 'class' => 'btn btn-primary btn-sm',
]);
},
 'delete' => function ($url, $model, $key) {
 return Html::a('<span class="fa fa-trash"></span>', $url, [
 'title' => Yii::t('backend', 'Delete'),
 'data-confirm' => 'Are you sure you want to delete?',
 'class' => 'btn btn-danger btn-sm',
]);
},
],
],


Try using

<?php
namespace common\components;

use \yii\grid\Actioncolumn;
use \yii\helpers\Html;

class ActionColumn extends ActionButton {

function init() {
parent::init();
$this--->initDefaultButtons();
}

protected function initDefaultButtons()
{
 $this->buttons['view'] = function ($url, $model, $key) {
 return Html::a('<span class="fa fa-eye"></span>', $url, [
 'title' => Yii::t('backend', 'View'),
 'class' => 'btn btn-success btn-sm',
]);
};
}

}


but it turns out some garbage. The error in the error.
How did the mind move duplicate code in other files?
July 9th 19 at 13:21
1 answer
July 9th 19 at 13:23
You have the usual array of buttons, the same as in params.php (the configs).
Nobody forbids to make also in this situation.

In other cases, the code duplication may be treated differently.

Find more questions by tags Yii