I was looking for a answer over the internet and discussed with my partners, but still not sure about the best option to return json in yii2 controller. Here the options:
public function actionExample (){//1
// do something whit $data result ...
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return $data;
}
public function actionExample (){//2
// do something whit $data result ...
echo json_encode($data);
Yii::$app->end();
}
I think the first option is the best (more elegant) for example for RESTful controller. However, if im not sure if all the calls to controller can receive json, or if some calls are asynchronus maybe the second option is the best, bocause stop the ejecution. I hope some one can explain the diferrence advantages and disadvantages of each method