Comment obtenir une réponse au format JSON (application / JSON) à yu?
Question
Comment obtenir une réponse au format JSON (application / JSON) dans yu?
La solution
Pour Yii 1:
Créer cette fonction dans votre (base) Contrôleur:
/**
* Return data to browser as JSON and end application.
* @param array $data
*/
protected function renderJSON($data)
{
header('Content-type: application/json');
echo CJSON::encode($data);
foreach (Yii::app()->log->routes as $route) {
if($route instanceof CWebLogRoute) {
$route->enabled = false; // disable any weblogroutes
}
}
Yii::app()->end();
}
Ensuite, il suffit appeler à la fin de votre action:
$this->renderJSON($yourData);
Pour 2 Yii:
2 Yii a cette fonctionnalité intégrée , utilisez le code suivant à la fin de votre action de commande:
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return $data;
Autres conseils
$this->layout=false;
header('Content-type: application/json');
echo CJavaScript::jsonEncode($arr);
Yii::app()->end();
Pour Yii2 l'intérieur d'un contrôleur:
public function actionSomeAjax() {
$returnData = ['someData' => 'I am data', 'someAnotherData' => 'I am another data'];
$response = Yii::$app->response;
$response->format = \yii\web\Response::FORMAT_JSON;
$response->data = $returnData;
return $response;
}
$this->layout=false;
header('Content-type: application/json');
echo json_encode($arr);
Yii::app()->end();
class JsonController extends CController {
protected $jsonData;
protected function beforeAction($action) {
ob_clean(); // clear output buffer to avoid rendering anything else
header('Content-type: application/json'); // set content type header as json
return parent::beforeAction($action);
}
protected function afterAction($action) {
parent::afterAction($action);
exit(json_encode($this->jsonData)); // exit with rendering json data
}
}
class ApiController extends JsonController {
public function actionIndex() {
$this->jsonData = array('test');
}
}
d'une façon plus simple en utilisant
echo CJSON::encode($result);
exemple de code:
public function actionSearch(){
if (Yii::app()->request->isAjaxRequest && isset($_POST['term'])) {
$models = Model::model()->searchNames($_POST['term']);
$result = array();
foreach($models as $m){
$result[] = array(
'name' => $m->name,
'id' => $m->id,
);
}
echo CJSON::encode($result);
}
}
hourras:)
Yii::app()->end()
Je pense que cette solution est la meilleure façon de flux d'application finale, car il utilise la fonction de exit()
de PHP, sorcière signifie sortie immédiate du flux d'exécution. Oui, il est le gestionnaire de onEndRequest
de Yû et la register_shutdown_function
de PHP, mais il reste encore trop fataliste.
Pour moi, la meilleure façon est ce
public function run($actionID)
{
try
{
return parent::run($actionID);
}
catch(FinishOutputException $e)
{
return;
}
}
public function actionHello()
{
$this->layout=false;
header('Content-type: application/json');
echo CJavaScript::jsonEncode($arr);
throw new FinishOutputException;
}
Ainsi, le flux de l'application continue d'exécuter même après.