单独讲一下laravel跟Dingo结合编写api及api路由配置文件的作用

管理员 发布于 4年前   462

单独讲一下这个api配置文件(我的接口路由都写这里):

\vendor\wanglelecc\laracms-framework\routes\api.php

截取一些常用的配置项及其作用 做注释
$api = app('Dingo\Api\Routing\Router');
/** v1 Version API Routes */
$api->version('v1', [
        //命名空间 定义我的api的控制器
    'namespace' => 'Wanglelecc\Laracms\Http\Controllers\Api\V1',
    'middleware' => 'serializer:array',
], function($api) {
        //api分组编写 字面意思相同的写到一起 方面维护管理及控制
    $api->group([
            //中间件
        'middleware' => 'api.throttle',
        //接口频率限制 次数
        'limit' => config('api.rate_limits.sign.limit'),
        //接口频率限制 时间(这里是时间 分钟)
        'expires' => config('api.rate_limits.sign.expires'),
    ], function($api){
        #里面就是接口路由了
        $api->post('verificationCodes', 'VerificationCodesController@store')->name('api.verificationCodes.store');# 短信验证码
        $api->post('users', 'UsersController@store')->name('api.users.store');# 用户注册
        $api->post('captchas', 'CaptchasController@store')->name('api.captchas.store'); # 图片验证码
        $api->post('socials/{social_type}/authorizations', 'AuthorizationsController@socialStore')->name('api.socials.authorizations.store');# 第三方登录
        $api->post('authorizations', 'AuthorizationsController@store')->name('api.authorizations.store');# 登录
        $api->put('authorizations/current', 'AuthorizationsController@update')->name('api.authorizations.update');# 刷新token
        $api->delete('authorizations/current', 'AuthorizationsController@destroy')->name('api.authorizations.destroy');# 删除token
        $api->get('cardnewsarticles/{category_id}', 'CardNewsArticleController@index')->name('api.article.index');
        $api->get('cardnewssearcharticles', 'CardNewsArticleController@search');
        $api->get('loansnewsarticles/{category_id}', 'CardNewsArticleController@loansart')->name('api.article.loansart');
        $api->get('loansnewssearcharticles', 'CardNewsArticleController@loanssearch');
        # 获取区块内容
        $api->get('blocks/{block_id}', 'BlockController@show')->name('api.block.show');
        //评论
        $api->post('comment', 'CommentController@comment')->name('api.comment.comment');
        $api->get('commentpages', 'CommentController@commentpages')->name('api.comment.commentpages');
        //up/dowm
        $api->get('creditcardupdown', 'CommentController@creditcardupdown')->name('api.comment.creditcardupdown');
    });

    //第二个api分组了 可以对应不同的接口频率限制  access
    $api->group([
        'middleware' => 'api.throttle',
        'limit' => config('api.rate_limits.access.limit'),
        'expires' => config('api.rate_limits.access.expires'),], function($api) {
        //AB版
        $api->get('cbbab', 'DbgController@ab')->name('api.dbg.ab');
        //小程序
        $api->get('xcxkfstatus', 'XcxController@xcxkfstatus')->name('api.xcx.xcxkfstatus');
        $api->get('topcarousel', 'XcxController@topcarousel')->name('api.xcx.topcarousel');
        $api->get('centericon', 'XcxController@centericon')->name('api.xcx.centericon');
        $api->get('xcxcentericonid', 'XcxController@xcxcentericonid')->name('api.xcx.xcxcentericonid');
        $api->get('xcxkfxx', 'XcxController@xcxkfxx')->name('api.xcx.xcxkfxx');
        $api->post('xcxkfxx', 'XcxController@xcxkfxx')->name('api.xcx.xcxkfxx');
        //baidu
        $api->get('bxcxkfxx', 'XcxController@bxcxkfxx')->name('api.xcx.bxcxkfxx');
        $api->post('bxcxkfxx', 'XcxController@bxcxkfxx')->name('api.xcx.bxcxkfxx');
    });
});

接口频率限制 (上面的分组接口频率限制 时间次数就是读这里)  

配置文件路径:\config\api.php

    /** 接口频率限制 */
    'rate_limits' => [
        // 访问频率限制,次数/分钟
        'access' => [
            'expires' => env('RATE_LIMITS_EXPIRES', 1),
            'limit'  => env('RATE_LIMITS', 600),
        ],
        // 登录相关,次数/分钟
        'sign' => [
            'expires' => env('SIGN_RATE_LIMITS_EXPIRES', 1),
            'limit'  => env('SIGN_RATE_LIMITS', 10),
        ],
    ],

看到没有两个分组读的是 assess跟sign  

assess:config('api.rate_limits.access.limit')

sign:config('api.rate_limits.sign.limit')

看一下效果:

1.png

1.默认每分钟600次

2.png

2.设置成每分钟400次

image.png

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成

是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客

      订阅博客周刊

文章标签

友情链接

HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群
侯体宗的博客