1. Go to this page and download the library: Download bestyii/yii2-openapi-reader 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/ */
/**
* @OA\OpenApi(
* @OA\Info(
* version="0.0.1",
* title="OpenApi",
* description="This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the Bearer `access token` to test the authorization filters.",
* ),
* @OA\Server(
* description="Test",
* url="http://api.bestyii.com/api/"
* ),
* @OA\Server(
* description="Prod",
* url="http://api.bestyii.com/v2/"
* ),
* @OA\ExternalDocumentation(
* description="更多关于达卡拉的信息",
* url="http://bestyii.com"
* )
* )
*/
/**
* @OA\SecurityScheme(
* securityScheme="bearerAuth",
* type="http",
* scheme="bearer",
* in="header",
* bearerFormat="JWT"
* )
* https://swagger.io/docs/specification/authentication/basic-authentication/
*/
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.');
}
}