Wie Antwort als JSON-Format (application / json) in yii zu bekommen?
Frage
Wie kommt man Antwort als JSON-Format (application / json) in yii?
Lösung
Für Yü 1:
Erstellen Sie diese Funktion in Ihrem (Basis) Controller:
/**
* 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();
}
Dann rufen Sie einfach am Ende Ihrer Aktion:
$this->renderJSON($yourData);
Für Yü 2:
Yü 2 diese Funktionalität verfügt über eine integrierte in , am Ende Ihrer Controller-Aktion den folgenden Code verwenden:
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return $data;
Andere Tipps
$this->layout=false;
header('Content-type: application/json');
echo CJavaScript::jsonEncode($arr);
Yii::app()->end();
Für Yii2 in einem Controller:
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');
}
}
eine einfache Art und Weise durch die Verwendung
echo CJSON::encode($result);
Beispielcode:
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);
}
}
cheers:)
Yii::app()->end()
Ich denke, diese Lösung ist nicht der beste Weg, um End-Anwendung fließen, weil es PHP exit()
Funktion verwendet, bedeutet Hexe sofortigen Ausstieg aus Ausführungsablauf. Ja, es gibt Yü der onEndRequest
Handler und PHP register_shutdown_function
aber es bleibt noch zu fatalistisch.
Für mich der bessere Weg ist, diese
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;
}
So ist der Anwendungsfluss wird fortgesetzt, auch nachdem auszuführen.