thinkphp5开发restful-api接口 学习笔记一

视频学习地址:php

http://study.163.com/course/courseMain.htm?courseId=1004171002html

源码和文档(若是满意,欢迎 star):mysql

https://github.com/RiversCoder/tp5-apigit

百度云盘连接:https://pan.baidu.com/s/1jMNumEOJ2yO5kSKYfnGjOw 密码:l8qrgithub

看云文档:
学习笔记:
sublime 3下载地址:
 
thinkphp的报错信息只支持,string,数字,空,回调函数,不支持数组
 若是要返回数组格式的数据,须要用json或者json_encode();一下
 1 <?php  2 namespace app\index\controller;  3 class Index {  4 public function index() {  5 $data = array(  6 'name' => 'red_panda',  7 'address' => 'China',  8  );  9 $code = 200; 10 $msg = 'ok'; 11 return json_encode(['data' => $data, 'code' => $code, 'message' => $msg]); 12  } 13 }

 config.php里能够改输出的类型(这样就能够直接return array了).sql

'default_return_type'=>'json' // html/json/xml/thinkphp


 获取参数:

 1 <?php  2 namespace app\index\controller;  3 use \think\Request;  4 class Index {  5 public function index() {  6 $request = Request::instance();  7 echo '请求方法:' . $request->method() . '<br/>';  8 echo '访问地址:' . $request->ip() . '<br/>';  9 echo '请求参数:'; 10 dump($request->param()); 11 echo '请求参数:仅包含name,sex'; 12 dump($request->only(['name', 'sex'])); 13 echo '请求参数:排除name,sex'; 14 dump($request->except(['name', 'sex'])); 15  } 16 }

效果:数据库

 postman post请求方法:json

返回参数:api

thinkphp5里判断请求方法:

 1 <?php  2 namespace app\index\controller;  3 use \think\Request;  4 class Test {  5 public function index() {  6 $request = Request::instance();  7 // 是否为 GET 请求  8 if (Request::instance()->isGet()) echo "当前为 GET 请求";  9 // 是否为 POST 请求 10 if (Request::instance()->isPost()) echo "当前为 POST 请求"; 11 // 是否为 PUT 请求 12 if (Request::instance()->isPut()) echo "当前为 PUT 请求"; 13 // 是否为 DELETE 请求 14 if (Request::instance()->isDelete()) echo "当前为 DELETE 请求"; 15 // 是否为 Patch 请求 16 if (Request::instance()->isPatch()) echo "当前为 PATCH 请求"; 17  } 18 }

 参数验证规则:

 1 <?php  2 namespace app\index\controller;  3 use \think\Validate;  4 class Test {  5 public function index() {  6 $rule = [  7 //utf-8 一个字符对应3个字母/数字 对应2个汉字(因此这里能够入3个字母/数字或者一个汉字)  8 'name' => 'require|max:3',  9 'age' => 'number|between:1,120', 10 'email' => 'email', 11  ]; 12 $msg = [ 13 'name.require' => '名称必须', 14 'name.max' => '名称最多不能超过3个字符', 15 'age.number' => '年龄必须是数字', 16 'age.between' => '年龄只能在1-120之间', 17 'email' => '邮箱格式错误', 18  ]; 19 $data = input('post.'); 20 $validate = new Validate($rule, $msg); 21 $result = $validate->check($data); 22 if (!$result) { 23 dump($validate->getError()); 24  } 25  } 26 }

效果:

 **链接数据库**

/* 数据库设置 */ 'database' => [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'thinkphp', // 数据库用户名 'username' => 'root', // 数据库密码 'password' => '', // 数据库链接端口 'hostport' => '', // 数据库链接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => '', // 数据库调试模式 'debug' => false, ],
 **原生sql语句查询**
 1 <?php  2 namespace app\index\controller;  3 use think\Db;  4 class Index  5 {  6 public function index()  7  {  8 $res = Db::query('select version()');  9 return $res; 10  } 11 }

 设置路由:

相关文章
相关标签/搜索