Giter Site home page Giter Site logo

woerwin / thinkphp5-restfulapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leslin/thinkphp5-restfulapi

0.0 2.0 0.0 3.02 MB

基于tp5的restful-api风格接口,oauth2.0,接口版本管理

License: Other

PHP 97.01% PLpgSQL 0.32% Smarty 2.67%

thinkphp5-restfulapi's Introduction

ThinkPHP5 restfulapi

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

ThinkPHP5的运行环境要求PHP5.4以上。

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

目录结构

初始的目录结构如下:

www  WEB部署目录(或者子目录)
├─application           应用目录
│  ├─common             公共模块目录(可以更改)
│  ├─api                接口目录
│  │  ├─controller      控制器目录
│  │  │     ├─v1        版本1目录
|  |  |     ├─v2        版本2目录
│  │  ├─Api.php         授权基类
│  │  ├─Oauth.php       授权验证
│  │  ├─Send.php        返回格式
│  │  ├─model           模型目录
|  |      ├─model     
│  │  ├─view            视图目录
│  │  └─ ...            更多类库目录
│  │
│  ├─command.php        命令行工具配置文件
│  ├─common.php         公共函数文件
│  ├─config.php         公共配置文件
│  ├─route.php          路由配置文件
│  ├─tags.php           应用行为扩展定义文件
│  └─database.php       数据库配置文件
│
├─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类,会在初始化检测user控制器定义的访问限制 $restMethodList = 'get|post|put';
  • 在Api类中,会有方法checkAuth()检测用户是否有权限调用接口
  • checkAuth方法会调用Oauth类中的鉴权,$baseAuth = Factory::getInstance(\app\api\controller\OAuth::class);
  • 根据用户端传递过来的app_key获取缓存中的access_token,在进行对比,如果true,则可以调用user中的各种方法,否则返回不能调用原因
  • Oauth类中的具体请看代码
  • 生成access_token,缓存access_token等相关逻辑在v1/Token.php代码中,使用的是本地缓存,如果需要使用数据库或者redis请查询相关注释说明
  • 写的比较乱,直接看片段截图把。。。。

相关流程截图

  • 用户类

  • Api类

  • Oauth类

  • v1/Token类

  • 测试

快速创建一个restful控制器

cd 到项目根目录

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

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

其他说明

交流QQ群号:645233951

版权信息

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

更多细节参阅 LICENSE.txt

thinkphp5-restfulapi's People

Contributors

leslin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.