## 一、Session
* 要使用`Session`類必須使用門面方式(`think\facade\Session`)調用
* 新版本不支持操作原生`$_SESSION`數組和所有`session_`開頭的函數,只能通過Session類(或者助手函數)來操作
1、配置文件 session.php
~~~
return?[
????//?session?name
????'name'???????????=>?'PHPSESSID',
????//?SESSION_ID的提交變量,解決flash上傳跨域
????'var_session_id'?=>?'',
????//?驅動方式?支持file?cache
????'type'???????????=>?'file',
????//?存儲連接標識?當type使用cache的時候有效
????'store'??????????=>?null,
????//?過期時間
????'expire'?????????=>?1440,
????//?前綴
????'prefix'?????????=>?'',
];
~~~
2、開啟Session
* 中間件`app\middleware.php`文件
~~~
\think\middleware\SessionInit::class
~~~
3、設置
* session 支持多級數組操作
~~~
Session::set('name','歐陽克');
//?Session數組
Session::set('admin.name','歐陽克');
Session::set('admin.uid',1);
~~~
4、讀取
~~~
//?獲取全部Session
Session::all();
//?獲取Session中name的值
Session::get('name');
~~~
5、刪除
~~~
Session::delete('name');
~~~
6、取值并刪除
~~~
Session::pull('name');
~~~
7、登陸示例
* 新建login.php文件
~~~
namespace?app\controller;
use?think\facade\View;
use?think\facade\Db;
use?think\facade\Request;
use?think\facade\Session;
class?Login{
????public?function?index(){
????????if(Request::method()?==?'POST'){
????????????$all?=?Request::param();
????????????$admin?=?Db::table('shop_admin')->where('account',$all['account'])->find();
????????????if(empty($admin)){
????????????????echo?json_encode(['code'=>1,'msg'=>'未找到管理員']);
????????????????exit;
????????????}
????????????if(md5($all['pwd'])?!=?$admin['password']){
????????????????echo?json_encode(['code'=>1,'msg'=>'密碼錯誤']);
????????????????exit;
????????????}
????????????Session::set('uid',$admin['uid']);
????????????Session::set('account',$admin['account']);
????????????echo?json_encode(['code'=>0,'msg'=>'登陸成功'])?;
????????}else{
????????????$title?=?'商城';
????????????View::assign([
????????????????'title'??=>?$title
????????????]);
????????????return?View::fetch();
????????}
????}
}
~~~
* 新建Login/index.html文件
~~~
<!DOCTYPE?html>
<html>
<head>
????<title>登錄</title>
????<link?rel="stylesheet"?type="text/css"?href="/static/layui/css/layui.css">
????<script?type="text/javascript"?src="/static/layui/layui.js"></script>
</head>
<body?style="background:?#1E9FFF">
????<div?style="position:?absolute;?left:50%;top:50%;width:?500px;margin-left:?-250px;margin-top:?-200px;">
????????<div?style="background:?#ffffff;padding:?20px;border-radius:?4px;box-shadow:?5px?5px?20px?#444444;">
????????????<form?class="layui-form">
????????????????<div?class="layui-form-item"?style="color:gray;">
????????????????????<h2>{$title}--后臺管理系統</h2>
????????????????</div>
????????????????<hr>
????????????????<div?class="layui-form-item">
????????????????????<label?class="layui-form-label">用戶名</label>
????????????????????<div?class="layui-input-block">
????????????????????????<input?type="text"?id="account"?class="layui-input">
????????????????????</div>
????????????????</div>
????????????????<div?class="layui-form-item">
????????????????????<label?class="layui-form-label">密 碼</label>
????????????????????<div?class="layui-input-block">
????????????????????????<input?type="password"?id="password"?class="layui-input">
????????????????????</div>
????????????????</div>
????????????????<div?class="layui-form-item">
????????????????????<div?class="layui-input-block">
????????????????????????<button?type="button"?class="layui-btn"?onclick="dologin()">登錄</button>
????????????????????</div>
????????????????</div>
????????????</form>
????????</div>
????</div>
????<script?type="text/javascript">
????????layui.use(['layer'],function(){
????????????$?=?layui.jquery;
????????????layer?=?layui.layer;
????????????//?用戶名控件獲取焦點
????????????$('#account').focus();
????????????//?回車登錄
????????????$('input').keydown(function(e){
????????????????if(e.keyCode?==?13){
????????????????????dologin();
????????????????}
????????????});
????????});
????????function?dologin(){
????????????var?account?=?$.trim($('#account').val());
????????????var?pwd?=?$.trim($('#password').val());
????????????if(account?==?''){
????????????????layer.alert('請輸入用戶名',{icon:2});
????????????????return;
????????????}
????????????if(pwd?==?''){
????????????????layer.alert('請輸入密碼',{icon:2});
????????????????return;
????????????}
????????????$.post('/index.php/login/index',{'account':account,'pwd':pwd},function(res){
????????????????if(res.code>0){
????????????????????layer.alert(res.msg,{icon:2});
????????????????}else{
????????????????????layer.msg(res.msg);
????????????????????setTimeout(function(){window.location.href?=?'/index.php/index/index'},1000);
????????????????}
????????????},'json');
????????}
????</script>
</body>
</html>
~~~
* index/index.html文件
~~~
use?think\facade\Session;
public?function?index(){
????$title?=?'商城';
????$session?=?Session::all();
????if(empty($session['uid'])){
????????echo?'<script?type="text/javascript">alert("請登錄!");window.location.href?=?"/index.php/login/index";?</script>';
????????exit;
????}
????$login?=?$session['account'];
????#?左側菜單
????$menu?=?Db::table('shop_menu')->where('fid',0)->select();
????$left?=?$menu->toArray();
????foreach($left?as?&$left_v){
????????$left_v['lists']?=?Db::table('shop_menu')->where('fid',$left_v['id'])->select();
????}
????#?右側列表
????$param?=?Request::param();
????if(isset($param['status'])?&&?$param['status']?==?1){
????????$where['status']?=?1;
????}else?if(isset($param['status'])?&&?$param['status']?==?2){
????????$where['status']?=?2;
????}else{
????????$where?=?true;
????}
????$p?=?isset($param['p'])???$param['p']?:?1;
????$db?=?new?Goods();
????$order?=?[
????????'add_time?DESC',
????????'id?DESC'
????];
????$right?=?$db->get_all($where,$order,$p,5);
????View::assign([
????????'title'??=>?$title,
????????'login'?=>?$login,
????????'left'?=>?$left,
????????'right'?=>?$right['data'],
????????'count'?=>?$right['count'],
????????'p'?=>?$p,
????????'status'?=>?isset($param['status'])???$param['status']?:?0
????]);
????return?View::fetch();
}
~~~
## 二、Cookie
* 要使用Cookie類必須使用門面方式(`think\facade\Cookie`)調用
* 配置文件位于配置目錄下的cookie.php文件,無需手動初始化,系統會在調用之前自動進行Cookie初始化工作
1、使用 Cookie
~~~
//?設置Cookie?有效期為?3600秒
Cookie::set('name',?'歐陽克',?3600);
//?永久保存Cookie
Cookie::forever('name',?'歐陽克');
//刪除cookie
Cookie::delete('name');
//?讀取某個cookie數據
Cookie::get('name');
~~~
2、Cookie 配置文件
* config 目錄下 cookie.php 文件
~~~
return?[
????//?cookie?保存時間
????'expire'????=>?0,
????//?cookie?保存路徑
????'path'??????=>?'/',
????//?cookie?有效域名
????'domain'????=>?'',
????//??cookie?啟用安全傳輸
????'secure'????=>?false,
????//?httponly設置
????'httponly'??=>?false,
????//?是否使用?setcookie
????'setcookie'?=>?true,
];
~~~
## 三、緩存
* 要使用緩存必須使用門面方式(`think\facade\Cache`)調用
* 內置支持的緩存類型包括`file`、`memcache`、`wincache`、`sqlite`、`redis`
1、使用緩存
~~~
//?緩存在3600秒之后過期
Cache::set('number',?10,?3600);
//?number自增(步進值為3)
Cache::inc('number',3);
//?number自減(步進值為1)
Cache::dec('number');
//?獲取緩存
Cache::get('number');
//?刪除緩存
Cache::delete('number');
//?push?追加緩存
Cache::set('name',?['歐陽克','朱老師']);
Cache::push('name',?'西門大官人');
//?獲取并刪除緩存
Cache::pull('name');
//?清空緩存
Cache::clear();
~~~
2、緩存配置文件
* config 目錄下 cache.php 文件
~~~
return?[
????//?默認緩存驅動
????'default'?=>?'file',
????//?緩存連接方式配置
????'stores'??=>?[
????????'file'?=>?[
????????????//?驅動方式
????????????'type'???????=>?'File',
????????????//?緩存保存目錄
????????????'path'???????=>?'',
????????????//?緩存前綴
????????????'prefix'?????=>?'',
????????????//?緩存有效期?0表示永久緩存
????????????'expire'?????=>?0,
????????????//?緩存標簽前綴
????????????'tag_prefix'?=>?'tag:',
????????????//?序列化機制?例如?['serialize',?'unserialize']
????????????'serialize'??=>?[],
????????],
????????//?redis緩存
????????'redis'???=>??[
????????????//?驅動方式
????????????'type'???=>?'redis',
????????????//?服務器地址
????????????'host'???=>?'127.0.0.1',
????????],
????????//?更多的緩存連接
????],
];
~~~
## 四、公用控制器
* `BaseController.php`默認基礎控制器類
~~~
use?think\facade\View;
use?think\facade\Db;
use?think\facade\Session;
public?function?initialize(){
????$session?=?Session::all();
????if(empty($session['uid'])){
????????echo?'<script?type="text/javascript">alert("請登錄!");window.location.href?=?"/index.php/login/index";?</script>';
????????exit;
????}
????$login?=?$session['account'];
????#?左側菜單
????$menu?=?Db::table('shop_menu')->where('fid',0)->select();
????$left?=?$menu->toArray();
????foreach($left?as?&$left_v){
????????$left_v['lists']?=?Db::table('shop_menu')->where('fid',$left_v['id'])->select();
????}
????View::assign([
????????'login'?=>?$login,
????????'left'?=>?$left,
????]);
}
Index/Index.php
namespace?app\controller;
use?app\BaseController;
use?think\facade\View;
use?think\facade\Db;
use?think\facade\Request;
use?app\model\Goods;
class?Index?extends?BaseController{
????public?function?index(){
????????$title?=?'商城';
????????#?右側列表
????????$param?=?Request::param();
????????if(isset($param['status'])?&&?$param['status']?==?1){
????????????$where['status']?=?1;
????????}else?if(isset($param['status'])?&&?$param['status']?==?2){
????????????$where['status']?=?2;
????????}else{
????????????$where?=?true;
????????}
????????$p?=?isset($param['p'])???$param['p']?:?1;
????????$db?=?new?Goods();
????????$order?=?[
????????????'add_time?DESC',
????????????'id?DESC'
????????];
????????$right?=?$db->get_all($where,$order,$p,5);
????????View::assign([
????????????'title'??=>?$title,
????????????'right'?=>?$right['data'],
????????????'count'?=>?$right['count'],
????????????'p'?=>?$p,
????????????'status'?=>?isset($param['status'])???$param['status']?:?0
????????]);
????????return?View::fetch();
????}
}
~~~
## 五、門面 Facade
* 門面為容器中的(動態)類提供了一個靜態調用接口,相比于傳統的靜態方法調用, 帶來了更好的可測試性和擴展性,你可以為任何的非靜態類庫定義一個`facade`類
(動態)類庫Facade類think\Appthink\facade\Appthink\Cachethink\facade\Cachethink\Configthink\facade\Configthink\Cookiethink\facade\Cookiethink\Dbthink\facade\Dbthink\Env think\facade\Envthink\Eventthink\facade\Eventthink\Filesystemthink\facade\Filesystemthink\Lang think\facade\Langthink\Log think\facade\Logthink\Middlewarethink\facade\Middlewarethink\Requestthink\facade\Requestthink\Responsethink\facade\Responsethink\Routethink\facade\Routethink\Sessionthink\facade\Sessionthink\Validatethink\facade\Validatethink\View think\facade\View
?1、Facade類
* 門面為容器中的(動態)類提供了一個靜態調用接口,相比于傳統的靜態方法調用,帶來了更好的可測試性和擴展性
* 系統已經為大部分核心類庫定義了Facade,所以你可以通過Facade來訪問這些系統類
~~~
use?think\facade\App;
use?think\facade\Db;
use?think\facade\View;
use?think\facade\Request;
use?think\facade\Session;
class?Index{
????public?function?index(){
????????//?數據庫操作
????????$select?=?Db::table('shop_goods')->select();
????????//?請求對象
????????$param?=?Request::param();
????????//?Session
????????$session?=?Session::all();
????????//?視圖
????????return?View::fetch();
????}
}
~~~
2、(動態)類庫
~~~
use?think\App;
use?think\Db;
use?think\View;
use?think\Request;
use?think\Session;
class?Index{
????public?function?index(View?$view,Db?$db){
????????$select?=?$db->table('shop_goods')->select();
????????$view->assign([
????????????'name'?=>?'歐陽克',
????????????'select'?=>?$select
????????]);
????????return?$view->fetch();
????}
}
~~~
## 六、助手函數
* Thinkphp 系統為一些常用的操作方法封裝了助手函數
助手函數描述abort 中斷執行并發送HTTP狀態碼app 快速獲取容器中的實例 支持依賴注入bind 快速綁定對象實例cache 緩存管理class_basename 獲取類名(不包含命名空間)class_uses_recursive 獲取一個類里所有用到的traitconfig獲取和設置配置參數cookie Cookie管理download 獲取\think\response\Download對象實例dump 瀏覽器友好的變量輸出env 獲取環境變量event 觸發事件halt 變量調試輸出并中斷執行input 獲取輸入數據 支持默認值和過濾invoke 調用反射執行callable 支持依賴注入json JSON數據輸出jsonpJSONP數據輸出lang 獲取語言變量值parse_name 字符串命名風格轉換redirect 重定向輸出request 獲取當前Request對象response 實例化Response對象session Session管理token 生成表單令牌輸出trace 記錄日志信息trait_uses_recursive 獲取一個trait里所有引用到的traiturl Url生成validate 實例化驗證器view 渲染模板輸出display 渲染內容輸出xml XML數據輸出app_path 當前應用目錄base_path 應用基礎目錄config_path 應用配置目錄public_path web根目錄root_path 應用根目錄runtime_path 應用運行時目錄
>
> // 獲取輸入數據,跟Request::param()效果一樣
>
> $param = input();
>
> // 變量調試輸出并中斷執行
>
> $shop = Db::table('shop\_goods')->select();
>
> halt($shop);
>
> // 渲染模板輸出,跟View::fetch()效果一樣
>
> return view();
## 七、調試
1、調試模式 和 Trace調試
* 根目錄里`.env`文件
> // 開啟調試模式 和 Trace調試
>
> APP\_DEBUG = true
備:正式部署后關閉調試模式
2、變量調試
* ThinPHP內置了`dump`調試方法
> $shop = Db::table('shop\_goods')->select();
>
> dump($shop);