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