ThinkPHP6 开发文档实用手册:数据库、路由、验证、模板视图...
数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
use think\facade\Db; $rs =Db:: name ( 'user' )-> where ( 'id' ,1)->find(); 查询一条记录 name 不含前缀 $rs =Db:: table ( 'ims_user' )-> where ( 'sex' , 2)-> select (); 多条数据 table 含前缀 $rs1 =Db:: name ( 'user' )-> where ( 'id' , 1)->value( 'name' ); 查询某个字段值 $rs =Db:: table ( 'ims_user' )-> where ( 'sex' , 2)-> column ( 'name,id' , 'id' ); 返回 name ,id列,后面是 key $userId = Db:: name ( 'user' )->insertGetId($data);//插入数据返回id Db:: name ( 'user' ) ->limit(100) ->insertAll($data); 插入多条数据,分每次100 Db:: name ( 'user' ) -> where ( 'id' , 1) -> update ([ 'name' => 'thinkphp' ]); 更新 Db:: table ( 'think_user' )-> delete (1); Db:: table ( 'think_user' )-> delete ([1,2,3]); Db:: table ( 'think_user' )-> where ( 'id' ,1)-> delete (); Db:: name ( 'user' )-> delete ( true );//清空数据 where ( 'id' , '<>' ,1) 不等于1 > >= like where ( "id=:id and username=:name" , [ 'id' => 1 , 'name' => 'thinkphp' ]) field( 'id,title,content' ) 指定字段 limit(10,25) 第十条开始25条 单数字返回数据条数 page(1,10) 第一页十条 order ([ 'id' => 'desc' , 'sex' => 'desc' ]) 排序 group ( 'user_id,test_time' ) 分组 count () max ( 'id' ) min () avg () sum () 聚合函数 whereTime( 'birthday' , '>=' , '1970-10-1' ) 支持< = whereTime( 'create_time' , '-2 hours' ) 查询2小时 whereBetweenTime( 'create_time' , '2017-01-01' , '2017-06-30' ) 查询时间段 whereYear( 'create_time' ) 今年 whereYear( 'create_time' , '2018' ) last year 去年 whereMonth( 'create_time' ) last month 上月 2018-06 具体月份 whereWeek( 'create_time' ) last week 上周 whereDay( 'create_time' )今天 yesterday昨天 2018-11-1具体 Db::query( "select * from think_user where status=1" ); 原生查询 Db:: execute ( "update think_user set name='thinkphp' where status=1" );//更新插入删除 Db::query( "select * from think_user where id=? AND status=?" , [8, 1]);//绑定 $list = Db:: name ( 'user' )-> where ( 'status' ,1)->paginate(10); 分页每页10 |
请求变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
use think\facade\Request; Request::param( 'name' ); Request::param();全部请求变量 返回数组 Request::param([ 'name' , 'email' ]); 多个变量 Request::param( 'a' , '1' ) $a 不存在使用默认值1 Request::param( 'username' , '' , 'strip_tags' ); 参数过滤 去掉html标签 htmlspecialchars转换成实体入库 strtolower 小写 Request::header(); 请求头数组,支持单个 cookie input( "name" ); Request::session();获取 $_SESSION 变量 Request::cookie();获取 $_COOKIE 变量 Request::server();获取 $_SERVER 变量 Request::env();返回env数组 Request::file();获取 $_FILES 变量 Request::baseUrl(); /index/index Request::host(true); 域名:www.baidu.com,默认无参数包含端口:80 Request::url(1); 完整域名和地址 http: //tp6.api.shanliwawa.top:80/index/index Request::domain(1) http: //tp6.api.shanliwawa.top Request::time() 请求时间戳 Request::app() 应用名 index Request::controller() 控制器 Index 参数true小写 Request::action() 操作 index 参数true 小写 Request::method(true); 请求类型获取 GET isGet isPost isPut isDelete isAjax isMobile isHead 判断是否某种类型 Request::has( 'id' , 'get' ); 检测变量id是否存在 url( 'index/hello' , [ 'id' =>5, 'name' => '李白' ], 'do' ); http: //tp6.api.shanliwawa.top/index/hello/李白.do?id=5 url( 'index/hello#aa' ); 锚点 Cache::set( 'name' , $value , 3600); 1小时后过期 Cache::get( 'name' ); 获取缓存 多缓存类型配置 return [ // 缓存类型为File 'type' => 'redis' , // 全局缓存有效期(0为永久有效) ,开发下一定要设置-1 否在刷新后 还在 'expire' => -1, // 缓存前缀 'prefix' => 'think' , // 缓存目录 'host' => '127.0.0.1' , ]; return [ // 使用复合缓存类型 'type' => 'complex' , // 默认使用的缓存 'default' => [ // 驱动方式 'type' => 'file' , // 缓存保存目录 'path' => '../runtime/default' , ], // 文件缓存 'file' => [ // 驱动方式 'type' => 'file' , // 设置不同的缓存保存目录 'path' => '../runtime/file/' , ], // redis缓存 'redis' => [ // 驱动方式 'type' => 'redis' , // 服务器地址 'host' => '127.0.0.1' , ], ]; use think\facade\Cache; Cache::store( 'file' )->set( 'name' , '123' ,0); $v = Cache::store( 'redis' )->get( 'name' ); Cache::store( 'default' )->get( 'name' );文件缓存 Cache:: delete ( 'name' ); Cache::clear(); Cache::set( 'name' , [1,2,3]); Cache::push( 'name' , 4); Cache::remember( 'start_time' , time()); 不存在则创建 Cache::inc( 'name' ,1); 自增1 Cache::dec( 'name' ,1); 自减1 $redis = Cache::handler(); redis对象 配置redis session return [ 'type' => 'redis' , 'prefix' => 'think' , 'auto_start' => true, // redis主机 'host' => '127.0.0.1' , // redis端口 'port' => 6379, // 密码 'password' => '' , ] session( 'name' , [ 'thinkphp' ]); 设置支持字符串 数组 session( 'name' );获取 session( 'name' , null);删除 session(null);清空 cookie( 'name' , 'value' , 3600); 设置不支持数组,序列化后存储 cookie( 'name' ); cookie( 'name' , null); cookie( 'think_lang' , 'en-us' ); //设置语言类型 lang( 'add user error' ); //翻译 config( 'cache.type' ) 读取配置 |
验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
{:token_field()} 模板中输出令牌 {:token_meta()} ajax提交 $.ajaxSetup({ headers: { 'X-CSRF-TOKEN' : $( 'meta[name="csrf-token"]' ).attr( 'content' ) } }); Route::post( 'blog/save' , 'blog/save' )->token(); 路由中使用验证 think\facade\Validate $rule = [ 'name' => 'require|max:25' , 'age' => 'number|between:1,120' , 'email' => 'email' , ]; $msg = [ 'name.require' => '名称必须' , 'name.max' => '名称最多不能超过25个字符' , 'age.number' => '年龄必须是数字' , 'age.between' => '年龄只能在1-120之间' , 'email' => '邮箱格式错误' , ]; $data = [ 'name' => 'thinkphp' , 'age' => 10, 'email' => 'thinkphp@qq.com' , ]; $validate = Validate::rule( $rule )->message( $msg ); $result = $validate ->check( $data ); if (! $result ) { dump( $validate ->getError()); |
路由
1
2
3
4
5
6
|
Route::get( 'new/<id>' , 'News/read' ); // 定义GET请求路由规则 Route::post( 'new/<id>' , 'News/update' ); // 定义POST请求路由规则 Route::put( 'new/:id' , 'News/update' ); // 定义PUT请求路由规则 Route:: delete ( 'new/:id' , 'News/delete' ); // 定义DELETE请求路由规则 Route::any( 'new/:id' , 'News/read' ); // 所有请求都支持的路由规则 ->allowCrossDomain();跨 |
输出响应
1
2
3
4
5
6
7
|
$data =[ 'code' =>200, 'msg' => '信息提示' , 'list' =>[ '中国' ]]; json( $data ); jsonp( $data ); xml( $data ); redirect( 'http://www.thinkphp.cn' ); redirect( '/index/hello/name' ); //站内跳转 download( './static/2.xlsx' ); 下 |
定义全局常量
1
2
3
|
define( '__URL__' ,\think\facade\Request::domain(1)); http: //tp6.api.shanliwawa.top define( '__ROOT__' ,\think\facade\app::getRootPath()); 系统根目录 C:\www\tp6\ define( "PRE" ,config( 'database.prefix' )); 表前 |
绝对路径获取
1
2
3
4
|
\think\facade\app::getRootPath() 根目录C:\www\tp6\ \think\facade\app::getAppPath() 应用路径 C:\www\tp6\app\index\ \think\facade\app::getConfigPath() 配置路径C:\www\tp6\config\ \think\facade\app::version() 核心版 |
模板视图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
use think\facade\View; View::assign([ 'name' => 'ThinkPHP' , 'email' => 'thinkphp@qq.com' ]); View::assign( 'data' ,[ 'name' => 'ThinkPHP' , 'email' => 'thinkphp@qq.com' ]); View::fetch( 'index' ); 助手函数 view( 'index' , [ 'name' => 'ThinkPHP' , 'email' => 'thinkphp@qq.com' ]); 模板输出 { $name } { $data .name} 等价 { $data [ 'name' ]} {:dump( $data )} 使用函数 :开头 { $user .nickname| default = "这家伙很懒,什么也没留下" } { $Think .cookie.name} // 输出$_COOKIE['name']变量 { $Think .server.script_name} // 输出$_SERVER['SCRIPT_NAME']变量 { $Think .session.user_id} // 输出$_SESSION['user_id']变量 { $Think .get.page} // 输出$_GET['page']变量 { $Request .param.name} 获取name { $data .name|raw} 不转义输出 { $data .create_time| date = 'Y-m-d H:i' } {literal} Hello,{ $name }! 原样输出 {/literal} {load href= "/static/js/common.js,/static/js/common.css" /} 加载js,css {php} echo 'Hello,world!' ;{/php} { /* 注释内容 */ } 或 { // 注释内容 } { include file= "public/header" /} 模板包含 { include file= "Public/header" title= "$title" keywords= "开源WEB开发框架" /} 传入参数 { foreach $list as $key => $vo } { $vo .id}:{ $vo .name} {/ foreach } { for start= "开始值" end = "结束值" comparison= "" step= "步进值" name= "循环变量名" } {/ for } { if 表达式}value1 { elseif 表达式 /}value2 { else /}value3 {/ if } |
记录日志
1
2
3
4
5
6
7
|
log.php 可添加 'json' => 1 表示json格式 trace( "日志信息" ) app.php中 'app_trace' => true, trace.php改为默认html 'type' => 'Console' , |
上传
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
$file = request()->file( 'image' ); 移动到框架应用根目录/uploads/ 目录下 $info = $file ->move( '../uploads' ); if ( $info ){ 成功上传后 获取上传信息 输出 jpg echo $info ->getExtension(); 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg echo $info ->getSaveName(); 输出 42a79759f284b767dfcb2a0197904287.jpg echo $info ->getFilename(); } else { 上传失败获取错误信息 echo $file ->getError(); } 多文件xphr foreach ( $files as $file ){} 验证,生成带md5文件名 $info = $file ->rule( 'md5' )->validate([ 'size' =>15678, 'ext' => 'jpg,png,gif' ])->move( '../uploads' ); |
THE END