開啟debug除錯模式(正式上線建議關閉)
config.php
// 應用除錯模式
設定輸出型別
index.php
class index
public function index()
$data = ['name' => 'steven', 'age' => 24];
return json(['code' => 0, 'msg' => '操作成功', 'data' => $data]);
或者config.js
// 預設輸出型別, 可選html,json xml ...
'default_return_type' => 'json',
獲取請求引數
引入使用: use think\request;
use think\request;
class index {
public function index() {
$res = request::instance();
// 注意連線字串用'.',為不是'+'
echo '請求方法: ' . $res->method() . '
';';
echo '請求引數: ';
dump($res->param());
echo '請求引數(僅包含id): ';
dump($res->only(['id']));
echo '請求引數(排除id): ';
dump($res->except(['id']));
捕獲.png
判斷請求型別
if ($res->isget()) echo '這是get方法';
if ($res->ispost()) echo '這是post方法';
驗證引數資料
use think\validate;
$rules = [
'name' => 'require',
'age' => 'number|between:0,120',
'email' => 'email'
$msg = [
'name.require' => '姓名不能為空',
'age.number' => '年齡必須為數字型別',
'age.between' => '年齡範圍1-120',
'email' => '郵箱格式不正確'
// 注意post後有個點
$data = input('post.');
$validate = new validate($rules, $msg);
$res = $validate->check($data);
if(!$res){
echo $validate->geterror();
鏈結mysql資料庫
database.php
// | thinkphp [ we can do it just think ]
// | licensed ( )
// | author: liu21st
return [
// 資料庫型別
'type' => 'mysql',
// 伺服器位址
'hostname' => '127.0.0.1',
// 資料庫名
'database' => 'test',
// 使用者名稱
'username' => 'root',
// 密碼
'password' => '***',
index.php
use think\db;
$res = db::query('select * from user');
return $res;
090522 T 介面的用途
介面大致有如下用途 1.需求描述 在專案架構設計階段,應該基於需求分析,建立所有業務邏輯層及其以上的重要介面。包括imodel 模型,符合oo設計 imodelqueryer 查詢方法集,包含查詢用例 上層使用的查詢方法 iservice 用例服務 好處在於,後期實現時,不用再去考慮應該如何實現需求...
090522 T 介面的用途
介面大致有如下用途 1.需求描述 在專案架構設計階段,應該基於需求分析,建立所有業務邏輯層及其以上的重要介面。包括imodel 模型,符合oo設計 imodelqueryer 查詢方法集,包含查詢用例 上層使用的查詢方法 iservice 用例服務 好處在於,後期實現時,不用再去考慮應該如何實現需求...
介面鑑權之cookie session和token
一 鑑權 二 引入cookie session和token的原因 目前,大部分介面使用的都是http協議,而http協議是無狀態的,即本次請求和上一次請求是沒有任何關係的,無法共享資訊。比如,像我們現在 下單需要先登入,你登入成功了之後再去下單,伺服器怎麼知道你是已登入狀態呢?三 cookie和se...