PHP code example of myzero1 / yii2-rest
1. Go to this page and download the library: Download myzero1/yii2-rest 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/ */
myzero1 / yii2-rest example snippets
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' => '[email protected] ',
],
]
],
],
],
......
],
......
'components' => [
......
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [],
],
......
],
......
];
$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'
]
],
]
];
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'
],
];
}