1. Go to this page and download the library: Download bestyii/yii2-gii-rest library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bestyii / yii2-gii-rest example snippets
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'generators' => [ //自定义生成器
'rest-model' => [ // generator name
'class' => 'bestyii\giiRest\generators\model\Generator', // generator class
],
'rest-crud' => [ // generator name
'class' => 'bestyii\giiRest\generators\crud\Generator', // generator class
]
],
];
namespace app\modules\api\controllers;
use app\modules\api\models\UserIdentity;
use Yii;
use app\modules\api\models\User;
use yii\data\ActiveDataProvider;
use app\modules\api\components\ActiveController;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
/**
* @OA\Tag(
* name="Users",
* description="用户账号",
* @OA\ExternalDocumentation(
* description="更多相关",
* url="http://bestyii.com"
* )
* )
*/
class UserController extends ActiveController
{
public $modelClass = 'app\modules\api\models\UserIdentity';
/**
* @OA\Get(
* path="/users",
* summary="查询 User",
* tags={"Users"},
* description="",
* operationId="findUser",
* @OA\Parameter(
* name="ids",
* in="query",
* description="逗号隔开的 id",
* tionId="getUserById",
* tags={"Users"},
* @OA\Parameter(
* description="id",
* in="path",
* name="id",
* mediaType="application/x-www-form-urlencoded",
* @OA\Schema(
* type="object",
* ref="#/components/schemas/User"
* ),
* )
* ),
* @OA\Response(
* response=201,
* description="操作成功",
* @OA\JsonContent(ref="#/components/schemas/User")
* ),
* @OA\Response(
* response=405,
* description="无效的输入",
* ),
* security={{
* "bearerAuth":{}
* }}
* )
*/
public function actionCreate()
{
$model = new UserIdentity();
if ($model->load(Yii::$app->getRequest()->getBodyParams(), '') && $model->save()) {
$response = Yii::$app->getResponse();
$response->setStatusCode(201);
} elseif (!$model->hasErrors()) {
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
}
return $model;
}
/**
* @OA\Put(
* path="/users/{id}",
* tags={"Users"},
* operationId="updateUserById",
* summary="更新指定ID数据",
* description="",
* @OA\Parameter(
* description="id",
* in="path",
* name="id",
* model = $this->findModel($id);
if ($model->softDelete() === false) {
throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
}
Yii::$app->getResponse()->setStatusCode(204);
}
/**
* Finds the User model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return User the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = UserIdentity::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested User does not exist.');
}
}