Yii2

How to set default parameters for a widget in Yii2
How to set default parameters for a widget in Yii2

To set default parameters for a widget in Yii2, you need to use the dependency injection container.

Example with DatePicker:

Yii::$container -> set('yiijuiDatePicker', [
	'language' => 'en-US',
]);

The first parameter in set is the class name, and the second is the array of parameters to configure. This way, we can preconfigure our widget properly from the start.

Your own Grid columns in the Yii2 framework
Your own Grid columns in the Yii2 framework

Continuing to expand my knowledge of Yii2.

I had, of course, heard about custom columns in Grid, but hadn’t used them in practice. However, when code duplication reached 5 separate places, I thought — why not give it a try? And it turned out to be quite nice, convenient, and most importantly — flexible!

Below is the full code listing of a sample column class that extends DataColumn:

namespace backendgrid;

use Yii;
use yiihelpersHtml;
use yiigridDataColumn;

class ActiveColumn extends DataColumn
{
	public $headerOptions = [
		'style' => 'width: 80px;'
	];
	
	public $contentOptions = [
		'class' => 'text-center'
	];
	
	public $attribute = 'is_active';
	
    /**
     * @inheritdoc
     */
    protected function renderDataCellContent($a)
    {
		if($a['is_active'] == 1)
		{
			return Html::a('<span class="glyphicon glyphicon-ok-circle"></span>', [
					'active', 'id' => $a['id'], 'is_active' => 0], [
					'title' => Yii::t('app', 'Unactivate this'),
					'data-pjax' => '0',
				]
			);
		}
		return Html::a('<span class="glyphicon glyphicon-lock"></span>', [
				'active', 'id' => $a['id'], 'is_active' => 1], [
				'title' => Yii::t('app', 'Activate this'),
				'data-pjax' => '0',
			]
		);
    }
}

read more...

How to write your own action in Yii2 and reuse it in different controllers
How to write your own action in Yii2 and reuse it in different controllers

Before developing any relatively large product using a framework, I would recommend deeply exploring its capabilities.

Today, we’ll talk about actions — but not the ones that are “hardcoded” into our controllers. Instead, we’ll look at actions that can be reused across different controllers by registering them through the controller’s actions() method:

public function actions()
{
	$actions = parent::actions();
	
	$actions['aj-avatar-upload'] = [
		'class' => 'commonactionuserAjaxAvatarUploadAction',
	];
	$actions['aj-avatar-delete'] = [
		'class' => 'commonactionuserAjaxAvatarDeleteAction',
	];
	
	return $actions;
}

You’ll encounter such "actions" early on, when looking at the configuration file where the error action is defined:

'errorHandler' => [
	'errorAction' => 'site/error',
],

read more...