WCO
WCO

Reputation: 480

Is it right to call a component function or its output in view form in yii2?

I am stuck in a situation where I am trying to get the output of a component to be shown in the view form as a dropdown. I cannot use model to store the data from the component output and show it in the view. As that will result in a huge data storage and might slow the application processing. The only option left is to directly use this component output in view.

Now, I am confused if I should call this output in controller and from controller I call the controller method in view similar to this: http://www.yiiframework.com/forum/index.php/topic/63169-way-to-call-controller-method-in-view-yii2/

The other way is to use JS in frontend and use the component output shown there in the view. Not sure if this is a good way either. What will be the right way to do this?

Upvotes: 1

Views: 564

Answers (1)

Yasin Patel
Yasin Patel

Reputation: 5731

You can use Component result in your view.

I am giving you an exapmle to get country code from component and store it in Model.

Ex.Function in Component.

public static function getDialCode()
{
  return   array(
    "+91"=>"India (+91)",
    "+62"=>"Indonesia (+62)",
    "+98"=>"Iran (+98)",
    "+39"=>"Italy (+39)",
  );
}

Function used in view for DropDown List

<?= $form->field($model, 'country_code')->dropDownList(Yii::$app->mycomponent->getDialcode(),['prompt'=>'Select']) ?>
// I registered component as mycomponent

Upvotes: 1

Related Questions