myzero1 / yii2-rest
Ti 是基于 yii2 的 restful API。
1.0.2
2019-03-07 11:42 UTC
Requires
- bower-asset/swagger-ui: 2.2.10
- yiisoft/yii2: ~2.0
- zircote/swagger-php: ^2.0
This package is not auto-updated.
Last update: 2024-09-13 16:43:27 UTC
README
在 yii2 中使用 wagger 的 restful API。
显示时间
安装
安装此模块的首选方式是通过 composer。
运行以下命令之一
php composer.phar require myzero1/yii2-rest:1.*
或者
"myzero1/yii2-rest": "~1"
将以下内容添加到您的 composer.json
文件的 require 部分。
设置
一旦安装了扩展,只需按照以下方式修改您的应用程序配置(main.php):
return [ ...... 'bootstrap' => [ ...... 'log', [ 'class' => 'backend\modules\v1\Bootstrap', 'params' => [ 'apiTokenExpire' => 1*24*3600, 'rateLimit' => [2000,3], // 2times/3s 'swaggerConfig' => [ 'schemes' => '{http}', 'host' => 'yii2rest3.test', 'basePath' => '/v1', 'info' => [ 'title' => '接口文档', 'version' => '1.0.0', 'description' => '这是关于: __react-admin__(https://github.com/marmelab/react-admin/tree/master/packages/ra-data-simple-rest)的rest api', 'contact' => [ 'name' => 'myzero1', 'email' => 'myzero1@sina.com', ], ] ], ], ], ...... ], ...... 'components' => [ ...... 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [], ], ...... ], ...... ];
按照以下方式设置 main-local.php 中的 gii:
$config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'class' => 'yii\gii\Module', 'allowedIPs' => ['*'], 'generators' => [ 'module-rest-swagger' => [ 'class' => 'myzero1\rest\gii\auth\Generator', 'templates' => [ 'rest' => 'myzero1\rest\gii\auth\default' ] ], 'obj-rest-swagger' => [ 'class' => 'myzero1\rest\gii\object\Generator', 'templates' => [ 'rest' => 'myzero1\rest\gii\object\default' ] ], ] ];
按照以下方式设置 siteController.php 中的 actions:
use yii\filters\AccessControl; use yii\helpers\Url; ...... /** * {@inheritdoc} */ public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => ['login', 'error'], 'allow' => true, ], [ 'actions' => ['logout', 'index', 'doc', 'api'], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'logout' => ['post'], ], ], ]; } ...... /** * {@inheritdoc} */ public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], 'doc' => [ 'class' => 'myzero1\rest\swaggertools\SwaggerAction', 'restUrl' => url::to(['/site/api'], true), ], 'api' => [ 'class' => 'myzero1\rest\swaggertools\SwaggerApiAction', 'scanDir' => [ Yii::getAlias('@vendor/myzero1/yii2-rest/src/swaggertools/config'), Yii::getAlias('@backend/modules/v1/swagger'), ], // 'api_key' => 'test' ], ]; }
使用方法
然后您可以访问 swagger 页面。
http://yii2rest2.test/site/doc
然后您可以访问 gii 页面以查看 rest 生成器。
http://yii2rest2.test/gii/default/view?id=api-rest