All Projects → Leslin → Thinkphp5 Restfulapi

Leslin / Thinkphp5 Restfulapi

Licence: other
restful-api风格接口 APP接口 APP接口权限 oauth2.0 接口版本管理 接口鉴权

Projects that are alternatives of or similar to Thinkphp5 Restfulapi

Graphql2rest
GraphQL to REST converter: automatically generate a RESTful API from your existing GraphQL API
Stars: ✭ 181 (-80.93%)
Mutual labels:  restful-api, restful, api-server
Perfect Ssm
🍇更完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis)
Stars: ✭ 606 (-36.14%)
Mutual labels:  restful-api, restful
Restful Api Design References
RESTful API 设计参考文献列表,可帮助你更加彻底的了解REST风格的接口设计。
Stars: ✭ 4,830 (+408.96%)
Mutual labels:  restful-api, restful
Go Zero
go-zero is a web and rpc framework written in Go. It's born to ensure the stability of the busy sites with resilient design. Builtin goctl greatly improves the development productivity.
Stars: ✭ 13,156 (+1286.3%)
Mutual labels:  restful-api, restful
Json Server Heroku
Deploy json-server to Heroku & Azure 🆙 🆓
Stars: ✭ 310 (-67.33%)
Mutual labels:  restful-api, api-server
Glances
Glances an Eye on your system. A top/htop alternative for GNU/Linux, BSD, Mac OS and Windows operating systems.
Stars: ✭ 19,648 (+1970.39%)
Mutual labels:  restful-api, restful
Koa2 Api Scaffold
一个基于Koa2的轻量级RESTful API Server脚手架。
Stars: ✭ 694 (-26.87%)
Mutual labels:  restful-api, restful
light-rest-4j
A RESTful framework built on top of light-4j with both Swagger 2.0 and OpenAPI 3.0 supports
Stars: ✭ 113 (-88.09%)
Mutual labels:  restful, restful-api
Apidoc
RESTful API 文档生成工具,支持 Go、Java、Swift、JavaScript、Rust、PHP、Python、Typescript、Kotlin 和 Ruby 等大部分语言。
Stars: ✭ 785 (-17.28%)
Mutual labels:  restful-api, restful
Ngx Restangular
Restangular for Angular 2 and higher versions
Stars: ✭ 787 (-17.07%)
Mutual labels:  restful-api, restful
Gen
Converts a database into gorm structs and RESTful api
Stars: ✭ 825 (-13.07%)
Mutual labels:  restful-api, restful
jooger.me
👍 My personal website,powered by @nuxt
Stars: ✭ 39 (-95.89%)
Mutual labels:  restful, restful-api
koa-rest-router
Most powerful, flexible and composable router for building enterprise RESTful APIs easily!
Stars: ✭ 67 (-92.94%)
Mutual labels:  restful, restful-api
Xmysql
xmysql is now https://github.com/nocodb/nocodb
Stars: ✭ 54 (-94.31%)
Mutual labels:  restful-api, api-server
rudder
RESTful API Proxy for Helm
Stars: ✭ 60 (-93.68%)
Mutual labels:  restful, api-server
Rest Api Design Guide
NBB's REST-ish API Design Guide
Stars: ✭ 643 (-32.24%)
Mutual labels:  restful-api, restful
Farwest
Framework for building RESTful HATEOAS-driven applications.
Stars: ✭ 18 (-98.1%)
Mutual labels:  restful-api, restful
REST-Api-with-Slim-PHP
REST API with PHP Slim Framework 3 and MySQL
Stars: ✭ 69 (-92.73%)
Mutual labels:  restful, restful-api
relax
Relax is a set of tools for modeling, documenting and testing RESTFul APIs
Stars: ✭ 12 (-98.74%)
Mutual labels:  restful, restful-api
Delphimvcframework
DMVCFramework (for short) is a popular and powerful framework for web solution in Delphi. Supports RESTful and JSON-RPC APIs development.
Stars: ✭ 761 (-19.81%)
Mutual labels:  restful-api, restful

ThinkPHP restfulapi

LICENSE

基于ThinkPHP5.1* 基础上开发的一个简单的restful api ,带权限验证等

ThinkPHP5.1的运行环境要求PHPPHP7.2+以上。

详细开发文档参考 ThinkPHP5完全开发手册

使用目前tp5.1相关新增功能,包含容器依赖注入、Facade、验证器等,与上一个版本相比,简化代码量,整个代码量只有不到200行,增加鉴权白名单,refresh_token、全局异常处理等

欢迎PR

老版本tp5.0*相关代码请到release下载对应代码

目录结构

初始的目录结构如下:

www  WEB部署目录(或者子目录)==
├─application           应用目录
│  ├─common             公共模块目录(可以更改)
│  ├─api                接口目录
│  │  ├─controller      控制器目录
│  │  │     ├─v1        版本1目录
|  |  |     ├─v2        版本2目录
│  │  ├─Api.php         授权基类
│  │  ├─Oauth.php       授权验证
│  │  ├─Send.php        返回格式
|  |  |─exception       重写异常
│  │  ├─model           模型目录
|  |      ├─model     
│  │  ├─view            视图目录
│  │  └─ ...            更多类库目录
│  │
│  ├─command.php        命令行工具配置文件
│  ├─common.php         公共函数文件
│  ├─config.php         公共配置文件
│  ├─route.php          路由配置文件
│  ├─tags.php           应用行为扩展定义文件
│  └─database.php       数据库配置文件
|─config                配置文件
|-routte                路由文件
├─public                WEB目录(对外访问目录)
│  ├─index.php          入口文件
│  ├─router.php         快速测试文件
│  └─.htaccess          用于apache的重写
│
├─thinkphp              框架系统目录
│  ├─lang               语言文件目录
│  ├─library            框架类库目录
│  │  ├─think           Think类库包目录
│  │  └─traits          系统Trait目录
│  │
│  ├─tpl                系统模板目录
│  ├─base.php           基础定义文件
│  ├─console.php        控制台入口文件
│  ├─convention.php     框架惯例配置文件
│  ├─helper.php         助手函数文件
│  ├─phpunit.xml        phpunit配置文件
│  └─start.php          框架入口文件
│
├─extend                扩展类库目录
├─runtime               应用的运行时目录(可写,可定制)
├─vendor                第三方类库目录(Composer依赖库)
├─build.php             自动生成定义文件(参考)
├─composer.json         composer 定义文件
├─LICENSE.txt           授权说明文件
├─README.md             README 文件
├─think                 命令行入口文件

流程

  • router.php中定义了restful资源路由,具体请查看代码。

  • 访问相应的url,例如:http://localhost/tp5test/public/index.php/v1/user

  • user控制器是继承了Api类

  • 在Api类中,会有方法init()检测用户是否有权限调用接口

  • init方法会调用Oauth类中的鉴权,$oauth = app('app\api\controller\Oauth');;

  • 根据用户端传递过来的app_key获取缓存中的access_token,在进行对比,如果true,则可以调用user中的各种方法,否则返回不能调用原因

  • Oauth类中的具体请看代码

  • 生成access_token,缓存access_token等相关逻辑在v1/Token.php代码中,使用的是本地缓存,如果需要使用数据库或者redis请查询相关注释说明

  • api端请求需要在header中进行authentication字段拼接,拼接规则:authentication:USERID base64_encode(appid:accesstoken:uid) PS:拼接示例:USERID c25haWx5X3hjeF9uZXc6M0U1TjBtR3dGSTZjZ1BoNEpLdHY4eWQyOVpqTXM3a2Y6Mzgw ,其中USERID后面跟了个空格,空格后面的一串字符串就是appid:accesstoken:uid这几个参数base64_encode后的字符串

  • uid 就是请求生成token时候返回

不知道header怎么传,请查看文档最后一张图,如果对参数不明白,请查看代码/Application/api/controller/Oauth.php中getClient方法,里面有逆向解密authentication值。

相关流程截图

流程图

截图

  • 路由设置 route\route.php

  • Api类

  • Oauth类

  • v1/Token类

截图

1、获取token传参

2、获取token、刷新的token,过期时间,用户信息

3、传递token进行请求

  • 加密头规则:USERID base64_encode(appid:token:uid)

快速创建一个restful控制器

cd 到项目根目录

命令行 :php think make:controller api/v1/Goods

修改路由,注册一个资源路由:在route.php加入下面一行代码: Route::resource(':version/goods','api/:version.Goods');

其他说明

交流QQ群号:645233951

版权信息

遵循Apache2开源协议发布,并提供免费使用。

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].