视频学习地址:
http://study.163.com/course/courseMain.htm?courseId=1004171002
源码和文档(如果满意,欢迎 star):
https://github.com/RiversCoder/tp5-api
百度云盘链接:https://pan.baidu.com/s/1jMNumEOJ2yO5kSKYfnGjOw 密码:l8qr
看云文档:
https://www.kancloud.cn/momingsixiali/thinkphp-resturl-api#/catalog
学习笔记:
http://f61be319.wiz03.com/share/s/3S6-cp1BIQ952yXKyj02PIM42a6LA83b6kll2FtQpG18ZOp6
sublime 3下载地址:
链接:https://pan.baidu.com/s/1YmAnF9c8HwV35gZOYVXHiw 密码:i4bx
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了).
'default_return_type'=>'json' // html/json/xml/
获取参数:
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请求方法:
返回参数:
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 }
设置路由:
[Haima的博客] http://www.cnblogs.com/haima/