/** * Creates a new Comment model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionApi($id = null) { $model = new Comment(); if ($id) { $model->thread = $id; } if ($model->load(Yii::$app->request->post()) && $model->save()) { echo json_encode(['success' => true, 'comment' => $model->asArray()]); } else { echo json_encode(['success' => false, 'error' => $model->errors]); } }
/** * Creates a new Comment model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionAdd($thread = null) { $model = new Comment(); if ($thread) { $model->thread = $thread; } Yii::$app->response->format = Response::FORMAT_JSON; if ($model->load(Yii::$app->request->post()) && $model->save()) { echo json_encode(['success' => true, 'comments' => [$model->asArray()]]); } else { echo json_encode(['success' => false, 'error' => $model->errors]); } }