graychen/yii2-gii-rest
Composer 安装命令:
composer require graychen/yii2-gii-rest
包简介
This is a resutful API creator for yii2
README 文档
README
Yii2 Code generator for REST api
Usage
Config gii modules in your application like this in backend\config\main-local.php
<?php $config = [ 'components' => [ 'request' => [ // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation 'cookieValidationKey' => '7Zahf41C4AMjxPsFuKs7m7rDwXrb-vNL', ], ], ]; if (!YII_ENV_TEST) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = [ 'class' => 'yii\debug\Module', 'allowedIPs' => ['*'], ]; $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'class' => 'yii\gii\Module', 'allowedIPs' => ['*'], 'generators' => [ 'api-rest' => [ 'class' => 'graychen\yii2\gii\rest\Generator', 'templates' => [ 'rest' => 'graychen\yii2\gii\rest\default' ] ] ] ]; } return $config;
Screen Shoot
Code Demo
Here is the Controller generated via the User model:
<?php namespace api\modules\v1\client; use Yii; use common\models\User; use yii\data\ActiveDataProvider; use yii\rest\ActiveController; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; /** * UserController implements the CRUD actions for User model. */ class UserController extends ActiveController { public $modelClass = 'common\models\User'; public function actions() { return []; } /** * Lists all User models. * @return mixed */ public function actionIndex() { $dataProvider = new ActiveDataProvider([ 'query' => User::find(), ]); return $dataProvider; } /** * Displays a single User model. * @param integer $id * @return mixed * @throws NotFoundHttpException if the model cannot be found */ public function actionView($id) { return $this->findModel($id), } /** * Creates a new User model. * @return mixed */ public function actionCreate() { $model = new User(); if ($model->load(Yii::$app->getRequest()->getBodyParams(), '') && $model->save()) { $response = Yii::$app->getResponse(); $response->setStatusCode(201); } elseif (!$model->hasErrors()) { throw new yii\web\ServerErrorHttpException('Failed to create the object for unknown reason.'); } return $model; } /** * Updates an existing User model. * @param integer $id * @return mixed * @throws NotFoundHttpException if the model cannot be found */ public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->response->setStatusCode(200); } elseif (!$model->hasErrors()) { throw new ServerErrorHttpException('Failed to update the object for unknown reason.'); } return $model; } /** * Deletes an existing User model. * @param integer $id * @return mixed * @throws NotFoundHttpException if the model cannot be found */ public function actionDelete($id) { $model = new User(); if ($this->findModel($id)->delete()) { Yii::$app->getResponse()->setStatusCode(204); } elseif (!$model->hasErrors()) { throw new ServerErrorHttpException('Failed to delete the object for unknown reason.'); } return $model; } /** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = User::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException('The requested User does not exist.'); } }
graychen/yii2-gii-rest 适用场景与选型建议
graychen/yii2-gii-rest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.29k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 graychen/yii2-gii-rest 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 graychen/yii2-gii-rest 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.29k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-12