bestyii/yii2-gii-rest
Composer 安装命令:
composer require bestyii/yii2-gii-rest
包简介
RESTful generator for gii
README 文档
README
Gii 模块的REST代码生成器,包含Model和Controller。
代码注释文档包含OpenAPI Specification 规范的文档。
可配合 Yii2 OpenApi Reader 模块渲染出漂亮的api文档。
安装 Installation
通过 composer安装.
项目中直接运行
php composer.phar require --prefer-dist bestyii/yii2-gii-rest "*"
or add
"bestyii/yii2-gii-rest": "*"
或者添加下面代码到 composer.json文件
使用 Usage
在config/web.php 配置文件中,gii的配置部分增加模版:
$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 ] ], ];
运行gii,可以看到增加了REST Model Generator和REST CRUD Generator两个生成器。
生成的代码示例:
示例 controller
<?php 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", * required=false, * @OA\Schema( * type="integer", * @OA\Items(type="int20"), * ), * ), * @OA\Response( * response=200, * description="查询成功", * @OA\Schema( * type="array", * @OA\Items(ref="#/components/schemas/User") * ), * ), * @OA\Response( * response="400", * description="无效的id", * ), * security={{ * "bearerAuth":{} * }} * ) */ public function actionIndex() { $dataProvider = new ActiveDataProvider([ 'query' => UserIdentity::find(), ]); return $dataProvider; } /** * @OA\Get( * path="/users/{id}", * summary="通过ID显示详情", * description="", * operationId="getUserById", * tags={"Users"}, * @OA\Parameter( * description="id", * in="path", * name="id", * required=true, * @OA\Schema( * type="integer", * format="int64" * ) * ), * @OA\Response( * response=200, * description="操作成功", * @OA\JsonContent(ref="#/components/schemas/User") * ), * @OA\Response( * response="400", * description="无效的ID" * ), * @OA\Response( * response="404", * description="没有找到相应资源" * ), * security={{ * "bearerAuth":{} * }} * ) */ public function actionView($id) { return $this->findModel($id); } /** * @OA\Post( * path="/users", * tags={"Users"}, * operationId="addUser", * summary="添加", * description="", * @OA\RequestBody( * required=true, * description="创建 User 对象", * @OA\JsonContent(ref="#/components/schemas/User"), * @OA\MediaType( * 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", * required=true, * @OA\Schema( * type="integer", * format="int64" * ) * ), * @OA\RequestBody( * required=true, * description="更新 User 对象", * @OA\JsonContent(ref="#/components/schemas/User"), * @OA\MediaType( * mediaType="multipart/form-data", * @OA\Schema(ref="#/components/schemas/User") * ) * ), * @OA\Response( * response=200, * description="操作成功", * @OA\JsonContent(ref="#/components/schemas/User") * ), * @OA\Response( * response=400, * description="无效的ID", * ), * @OA\Response( * response=404, * description="没有找到相应资源", * ), * @OA\Response( * response=405, * description="数据验证异常", * ), * security={{ * "bearerAuth":{} * }} * ) */ public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->getBodyParams(), '') && $model->save()) { Yii::$app->response->setStatusCode(200); } elseif (!$model->hasErrors()) { throw new ServerErrorHttpException('Failed to update the object for unknown reason.'); } return $model; } /** * @OA\Delete( * path="/users/{id}", * summary="删除User", * description="", * operationId="deleteUser", * tags={"Users"}, * @OA\Parameter( * description="需要删除数据的ID", * in="path", * name="id", * required=true, * @OA\Schema( * type="integer", * format="int64" * ) * ), * @OA\Response( * response=204, * description="没有找到相应资源" * ), * @OA\Response( * response=400, * description="无效的ID" * ), * @OA\Response( * response=404, * description="没有找到相应资源" * ), * security={{ * "bearerAuth":{} * }} * ) */ public function actionDelete($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.'); } }
示例 model
/** * @OA\Schema( * schema="User", * required={"username"}, * @OA\Property( * property="id", * description="User ID", * type="integer", * format="int64", * ), * @OA\Property( * property="username", * description="用户名", * type="string", * maxLength=100, * ), * @OA\Property( * property="email", * description="邮箱", * type="string", * maxLength=100, * ), * @OA\Property( * property="password", * description="密码", * type="string", * maxLength=64, * ), * @OA\Property( * property="created_at", * description="创建时间", * type="string", * default="0", * ), * @OA\Property( * property="updated_at", * description="更新时间", * type="string", * default="0", * ), * @OA\Property( * property="last_login_at", * description="最后登录时间", * type="string", * default="0", * ), * @OA\Property( * property="ip", * description="登录IP ip2long", * type="integer", * format="int64", * default=0, * ), *) */
bestyii/yii2-gii-rest 适用场景与选型建议
bestyii/yii2-gii-rest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.12k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2020 年 02 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「extension」 「swagger」 「yii2」 「gii」 「oas」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bestyii/yii2-gii-rest 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bestyii/yii2-gii-rest 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bestyii/yii2-gii-rest 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
Adds more BBCode
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 23.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2020-02-29
