Create function in MyComponent.php
namespace app\components;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use app\models\User;
class MyComponent extends Component
{
// Function return list of id & user Names,used for dropdownlist
public function getUserID()
{
$code = User::find()->select('id,name')
->where(['is_deleted'=>'n'])
->all();
$result = ArrayHelper::map($code, 'id', 'name');
if($result)
return $result;
else
return ["null"=>"No User"];
}
}
-> Register Component in web.php
components' => [
'mycomponent' => [
'class' => 'app\components\MyComponent',
],
]
-> use it in your view
<?= $form->field($model, 'user_id')->dropDownList(Yii::$app->mycomponent->getUserID())?>