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

491

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?

71

Answer

Solution:

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

People are also looking for solutions to the problem: php - htaccess to be used only in one folder

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.