**請求變量**
```
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') 讀取配置
```
**驗證**
```
{: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());
}
```
**路由**
```
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();跨域
```
***輸出響應***
```
$data=['code'=>200,'msg'=>'信息提示','list'=>['中國']];
json($data);
jsonp($data);
xml($data);
redirect('http://www.thinkphp.cn');
redirect('/index/hello/name'); //站內跳轉
download('./static/2.xlsx'); 下載
```
**數據庫**
```
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條
``
~~~
```
****模型
定義全局常量****
```
~~~
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')); 表前綴
~~~
```
**絕對路徑獲取**
```
~~~
\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() 核心版本
~~~
```
模板視圖
```
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}
```
**記錄日志**
```
log.php 可添加 'json' => 1 表示json格式
trace("日志信息")
app.php中
'app_trace' => true,
trace.php改為默認html
'type' => 'Console',
```
**上傳**
```
$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');
```
- thinkphp6執行流程(一)
- php中use關鍵字用法詳解
- Thinkphp6使用騰訊云發送短信步驟
- 路由配置
- Thinkphp6,static靜態資源訪問路徑問題
- ThinkPHP6.0+ 使用Redis 原始用法
- smarty在thinkphp6.0中的最佳實踐
- Thinkphp6.0 搜索器使用方法
- 從已有安裝包(vendor)恢復 composer.json
- tp6with的用法,表間關聯查詢
- thinkphp6.x多對多如何添加中間表限制條件
- thinkphp6 安裝JWT
- 緩存類型
- 請求信息和HTTP頭信息
- 模型事件用法
- 助手函數匯總
- tp6集成Alipay 手機和電腦端支付的方法
- thinkphp6使用jwt
- 6.0session cookie cache
- tp6筆記
- TP6(thinkphp6)隊列與延時隊列
- thinkphp6 command(自定義指令)
- command(自定義指令)
- 本地文件上傳
- 緩存
- 響應
- 公共函數配置
- 七牛云+文件上傳
- thinkphp6:訪問多個redis數據源(thinkphp6.0.5 / php 7.4.9)
- 富文本編輯器wangEditor3
- IP黑名單
- 增刪改查 +文件上傳
- workerman 定時器操作控制器的方法
- 上傳文件到阿里云oss
- 短信或者郵箱驗證碼防刷代碼
- thinkphp6:訪問redis6(thinkphp 6.0.9/php 8.0.14)
- 實現關聯多個id以逗號分開查詢數據
- thinkphp6實現郵箱注冊功能的細節和代碼(點擊鏈接激活方式)
- 用mpdf生成pdf文件(php 8.1.1 / thinkphp v6.0.10LTS )
- 生成帶logo的二維碼(php 8.1.1 / thinkphp v6.0.10LTS )
- mysql數據庫使用事務(php 8.1.1 / thinkphp v6.0.10LTS)
- 一,創建過濾IP的中間件
- 源碼解析請求流程
- 驗證碼生成
- 權限管理
- 自定義異常類
- 事件監聽event-listene
- 安裝與使用think-addons
- 事件與多應用
- Workerman 基本使用
- 查詢用戶列表按拼音字母排序
- 擴展包合集
- 查詢用戶數據,但是可以通過輸入用戶昵稱來搜索用戶同時還要統計用戶的文章和粉絲數
- 根據圖片的minetype類型獲取文件真實拓展名思路
- 到處excel
- 用imagemagick庫生成縮略圖
- 生成zip壓縮包并下載
- API 多版本控制
- 用redis+lua做限流(php 8.1.1 / thinkphp v6.0.10LTS )
- 【thinkphp6源碼分析三】 APP類之父, 容器Container類
- thinkphp6表單重復提交解決辦法
- 小程序授權
- 最簡單的thinkphp6導出Excel
- 根據訪問設備不同訪問不同模塊
- 服務系統
- 前置/后置中間件
- 給接口api做簽名驗證(php 8.1.1 / thinkphp v6.0.10LTS )
- 6實現郵箱注冊功能的細節和代碼(點擊鏈接激活方式)
- 使用前后端分離的驗證碼(thinkphp 6.0.9/php 8.0.14/vue 3.2.26)
- 前后端分離:用jwt+middleware做用戶登錄驗證(php 8.1.1 / thinkphp v6.0.10LTS )
- vue前后端分離多圖上傳
- thinkphp 分組、頁面跳轉與ajax
- thinkphp6 常用方法文檔
- 手冊里沒有的一些用法
- Swagger 3 API 注釋
- PHP 秒級定時任務
- thinkphp6集成gatewayWorker(workerman)實現實時監聽
- thinkphp6按月新增數據表
- 使用redis 實現消息隊列
- api接口 統一結果返回處理類
- 使用swoole+thinkphp6.0+redis 結合開發的登錄模塊
- 給接口api做簽名驗證
- ThinkPHP6.0 + UniApp 實現小程序的 微信登錄
- ThinkPHP6.0 + Vue + ElementUI + axios 的環境安裝到實現 CURD 操作!
- 異常$e
- 參數請求驗證自定義和異常錯誤自定義