diff --git a/README.md b/README.md index 4372728..9bd926b 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,22 @@ http://xxxxxxxxxxxxxx/topic/do?type=clap&model=topic&model_id=1 `model` recommend use `Model::tableName()` +http response success(code==200) return json: + +```json +{"code":200,"data":0,"message":"success"} +``` + + +http response failure(code==500) return json: + +```json +{"code":500,"data":"","message":"{\"model_id\":[\"Model ID不能为空。\"]}"} +``` + +Demo +------ + **ActiveDataProvider Demo 1** Controller diff --git a/src/actions/ActionAction.php b/src/actions/ActionAction.php index 16503e2..a54a594 100644 --- a/src/actions/ActionAction.php +++ b/src/actions/ActionAction.php @@ -9,6 +9,7 @@ use Yii; use yii\db\Exception; +use yii\helpers\Json; use yii\web\Response; use yiier\actionStore\models\ActionStore; @@ -25,14 +26,13 @@ public function run() if (Yii::$app->user->isGuest) { Yii::$app->getResponse()->redirect(\Yii::$app->getUser()->loginUrl)->send(); } else { + Yii::$app->response->format = Response::FORMAT_JSON; $model = new ActionStore(); $model->load(array_merge(Yii::$app->request->getQueryParams(), ['user_id' => Yii::$app->user->id]), ''); - $model->validate(); - if (!$model->errors) { - Yii::$app->response->format = Response::FORMAT_JSON; + if ($model->validate()) { return ['code' => 200, 'data' => ActionStore::createUpdateAction($model), 'message' => 'success']; } - return ['code' => 500, 'data' => '', 'message' => json_encode($model->errors)]; + return ['code' => 500, 'data' => '', 'message' => Json::encode($model->errors)]; } } } \ No newline at end of file diff --git a/src/models/ActionStore.php b/src/models/ActionStore.php index 3104738..86fbfb9 100644 --- a/src/models/ActionStore.php +++ b/src/models/ActionStore.php @@ -123,18 +123,6 @@ public static function createUpdateAction($model) throw new Exception(json_encode($model->errors)); } - /** - * @param $model ActionStore - * @return false|int - */ - public function destroyAction($model) - { - $data = $this->attributes; - unset($data['id'], $data['created_at'], $data['updated_at'], $data['value']); - return $model->delete(); - } - - /** * 返回计数器 * @return int