myzero1/yii2-apibycnf

Ti 是基于yii2的restful api。

安装: 6

依赖项: 0

建议者: 0

安全: 0

类型:yii2-module

0.0.2 2019-12-13 08:29 UTC

This package is not auto-updated.

Last update: 2020-08-07 12:11:49 UTC


README

您可以通过配置生成restfull api。

基于 yii2-apibycnf

显示时间

安装

安装此模块的首选方式是通过 composer

运行以下命令之一

php composer.phar require myzero1/yii2-apibycnf:*

"myzero1/yii2-apibycnf": "*"

将以下内容添加到您的 composer.json 文件的 require 部分。

设置

扩展安装后,只需按以下方式修改您的应用程序配置

在 main-local.php 中

...
if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
    ...
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
    ];
    $config['modules']['gii']['generators'] = [
        'api' => [
            'class' => 'myzero1\apibycnf\components\gii\generators\rest\Generator'
        ],
    ];
    ...
}
...

在 main.php 中

return [
    ......
    'bootstrap' => [
        ......
        'example',
        ......
    ],
    ......
    'modules' => [
        ......
        'apibycnf' => 'myzero1\apibycnf\Module',
        'example' => [
            'class' => 'myzero1\apibycnf\example\ApiByCnfModule',// should add table to db by 'yii2-apibycnf/src/user.sql'
            'apiTokenExpire' => 24 * 3600 * 365,
            'fixedUser' => [
                'id' => '1',
                'username' => 'myzero1',
                'api_token' => 'myzero1Token',
            ],
            'smsAndCacheComponents' => [
                'captchaCache' => [
                    'class' => '\yii\caching\FileCache',
                    'cachePath' => '@runtime/captchaCache',
                ],
                'captchaSms' => [
                    'class' => 'myzero1\smser\QcloudsmsSmser',// 腾讯云
                    'appid' => '1400280810', // 请替换成您的appid
                    'appkey' => '23e167badfc804d97d454e32e258b780', // 请替换成您的apikey
                    'smsSign' => '玩索得',
                    'expire' => '5',//分钟
                    'templateId' => 459670, // 请替换成您的templateId
                ],
            ],
            'runningAsDocActions' => [
                '*' => '*', // all ations, as default
                // 'controllerA' => [
                //     '*', // all actons in controllerA
                // ],
                // 'controllerB' => [
                //     'actionB',
                // ],
            ],
        ],
        ......
    ],
    ......
    'components' => [
        ......
        // 'assetManager' => [
        //     'class' => 'yii\web\AssetManager',
        //     'forceCopy' => true,// true/false
        // ],
        'user' => [
            // 'identityClass' => 'myzero1\apibycnf\components\rest\ApiAuthenticator',
            'identityClass' => 'myzero1\apibycnf\example\components\MyApiAuthenticator', // Rewrite
            'enableSession' => false,
            'authTimeout' => 3600 * 24, // defafult 24h
        ],
        ......
    ]
    ......
];

用法

可选模块

  • 设置 basePath 为 "/v2"
  • 点击 "预览" 按钮
  • 点击 "生成" 按钮,以生成代码。
  • 设置配置文件

在 main.php 中

return [
    ......
    'bootstrap' => [
        ......
        'v2',
        ......
    ],
    ......
    'modules' => [
        ......
        'v2' => [
            'class' => 'api\modules\v2\ApiByCnfModule',
            'apiTokenExpire' => 24 * 3600 * 365,
            'fixedUser' => [
                'id' => '1',
                'username' => 'myzero1',
                'api_token' => 'myzero1Token',
            ],
            'smsAndCacheComponents' => [
                'captchaCache' => [
                    'class' => '\yii\caching\FileCache',
                    'cachePath' => '@runtime/captchaCache',
                ],
                'captchaSms' => [
                    'class' => 'myzero1\smser\QcloudsmsSmser',// 腾讯云
                    'appid' => '1400280810', // 请替换成您的appid
                    'appkey' => '23e167badfc804d97d454e32e258b780', // 请替换成您的apikey
                    'smsSign' => '玩索得',
                    'expire' => '5',//分钟
                    'templateId' => 459670, // 请替换成您的templateId
                ],
            ],
            'runningAsDocActions' => [
                '*' => '*', // all ations, as default
                // 'controllerA' => [
                //     '*', // all actons in controllerA
                // ],
                // 'controllerB' => [
                //     'actionB',
                // ],
            ],
        ],
        ......
    ],
    ......
];
  • v2 将显示在可选模块菜单中,作为 v2 api
  • 您可以通过点击 v2 api 按钮来配置 v2
  • 您可以为返回特征添加 response_code 参数

restbyconfig 的其他菜单

  • 您可以点击 "Swagger" 按钮来使用它。
  • 您可以点击 "Markdown" 按钮来使用它。

可以覆盖类

  • myzero1\apibycnf\components\rest\Helper
  • myzero1\apibycnf\components\rest\ApiHelper
  • myzero1\apibycnf\components\rest\ApiAuthenticator
  • myzero1\apibycnf\components\rest\HandlingHelper

在 main.php 中

return [
    ......
    'bootstrap' => [
        ......
        'classMap' => function(){
             Yii::$classMap['myzero1\apibycnf\components\rest\Helper'] = '@app/modules/v1/components/Helper.php';
             Yii::$classMap['myzero1\apibycnf\components\rest\ApiHelper'] = 'path/to/ApiHelper.php';
        },
        ......
    ],
    ......
];

注意

  • 设置git避免,文件类型问题导致的换行符问题
  • 提交时转换为LF,检出时不转换 git config --global core.autocrlf input
  • 拒绝提交包含混合换行符的文件 git config --global core.safecrlf true
  • git-windows自带有dos2unix.exe, 执行 find . -type f -exec dos2unix {} \; 批量转换
  • 使用这个方便输出接收到的参数,file_put_contents(\Yii::getAlias('@runtime/captcha'), json_encode($input, JSON_UNESCAPED_UNICODE + JSON_PRETTY_PRINT) . "\n", FILE_APPEND);

变更日志

  • 添加我的组
  • 卡丁代码
  • 多个响应

待办事项