Action

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...