myzero1 / yii2-rest
Ti is for restful api by yii2.
Installs: 15
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-module
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-11-22 17:35:18 UTC
README
Rest api with wagger in yii2.
Show time
Installation
The preferred way to install this module is through composer.
Either run
php composer.phar require myzero1/yii2-rest:1.*
or add
"myzero1/yii2-rest": "~1"
to the require section of your composer.json
file.
Setting
Once the extension is installed, simply modify your application configuration(main.php) as follows:
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' => [], ], ...... ], ...... ];
Setting the gii in main-local.php as follows:
$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' ] ], ] ];
Setting the actions in siteController.php as follows:
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' ], ]; }
Usage
You can then access swagger page.
http://yii2rest2.test/site/doc
You can then access gii page to watch the rest generator.
http://yii2rest2.test/gii/default/view?id=api-rest