thinkPHP5框架接口寫法簡(jiǎn)單示例
本文實(shí)例講述了thinkPHP5框架接口寫法。分享給大家供大家參考,具體如下:
控制器
/**
* 添加收貨地址
*/
public function addAddress(){
$post = $this->request->post();
//驗(yàn)證 唯一規(guī)則: 表名,字段名,排除主鍵值,主鍵名
$validate = new \think\Validate([
['uid', 'require', '用戶id不能為空'],
['name', 'require|max:20', '收件人不能為空'],
['mobile', 'require|length:11', '手機(jī)號(hào)碼不能為空'],
['province_id', 'require', '省份不能為空'],
['city_id', 'require', '城市不能為空'],
['district_id', 'require', '縣區(qū)不能為空'],
['detail', 'require|max:100', '地址詳情不能為空'],
],[
'mobile.length' => '手機(jī)號(hào)碼格式不正確',
'name.max' => '收件人不能超過20個(gè)字符',
'detail.max' => '地址詳情不能超過100個(gè)字符',
]);
//驗(yàn)證部分?jǐn)?shù)據(jù)合法性
if (!$validate->check($post)) {
\Org\Response::show(400,'提交失?。? . $validate->getError());
}
$user_id = $post['uid'];
$name = $post['name'];
$mobile = $post['mobile'];
$province_id = $post['province_id'];
$city_id = $post['city_id'];
$district_id = $post['district_id'];
$detail = $post['detail'];
$is_address = model('address')->addAddress($user_id,$name,$mobile,$province_id,$city_id,$district_id,$detail);
if($is_address){
\Org\Response::show(200,'access!');
}else{
\Org\Response::show(400,'添加失敗!');
}
}
model
<?php
namespace app\index\model;
use \think\Model;
use app\index\model\Attachment as AttachmentModel;
class Address extends Model
{
/**
* 獲取一個(gè)基本信息
* @param int $id 行政id
* @return array|bool|false|\PDOStatement|string|Model
*/
public function adcodeGetOne($id = 0){
if(empty($id)) return false;
$map['adcode'] = $id;
return \think\Db::name('district')->where($map)->find();
}
/**
* @param $user_id 用戶id
* @param $name 收件人
* @param $mobile 收件人手機(jī)號(hào)
* @param $province_id 省行政id
* @param $city_id 城市行政id
* @param $district_id 縣區(qū)行政id
* @param $detail 詳細(xì)地址
*/
public function addAddress($user_id,$name,$mobile,$province_id,$city_id,$district_id,$detail){
$is_province = $this->adcodeGetOne($province_id);
$is_city = $this->adcodeGetOne($city_id);
$is_district= $this->adcodeGetOne($district_id);
if(empty($is_province)) \Org\Response::show(400,'無(wú)效省份!');
if(empty($is_city)) \Org\Response::show(400,'無(wú)效城市!');
if(empty($is_district)) \Org\Response::show(400,'無(wú)效縣區(qū)!');
$time = time();
$data['province_id'] =$province_id;
$data['province'] = $is_province['name'];
$data['city_id'] =$city_id;
$data['city'] = $is_city['name'];
$data['district_id'] =$district_id;
$data['district'] = $is_district['name'];
$data['detail'] =$detail;
$data['mobile'] =$mobile;
$data['name'] =$name;
$data['user_id'] =$user_id;
$data['is_delete'] = 0;
if($this->where($data)->field('id')->find()) return true;
$data['addtime'] =$time;
$data['update_time'] =$time;
if($this->insert($data)){
return true;
}else{
return false;
}
}
}
Response
<?php
namespace Org;
class Response {
const JSON = "json";
/**
* 按綜合方式輸出通信數(shù)據(jù)
* @param integer $code 狀態(tài)碼
* @param string $message 提示信息
* @param array $data 數(shù)據(jù)
* @param string $type 數(shù)據(jù)類型
* return string
*/
public static function show($code, $message = '', $data = array(), $type = self::JSON) {
if(!is_numeric($code)) {
return '';
}
// $type = 'json';
isset($_GET['format']) ? $_GET['format'] : self::JSON;
$result = array(
'code' => $code,
'message' => $message,
'data' => $data,
);
if($type == 'json') {
self::json($code, $message, $data);
exit;
} elseif($type == 'array') {
var_dump($result);
} elseif($type == 'xml') {
self::xmlEncode($code, $message, $data);
exit;
} else {
// TODO
}
}
/**
* 按json方式輸出通信數(shù)據(jù)
* @param integer $code 狀態(tài)碼
* @param string $message 提示信息
* @param array $data 數(shù)據(jù)
* return string
*/
public static function json($code, $message = '', $data = array()) {
if(!is_numeric($code)) {
return '';
}
$result = array(
'code' => $code,
'message' => urlencode($message),
'data' => $data
);
echo urldecode(json_encode($result,JSON_UNESCAPED_UNICODE));
exit;
}
/**
* 按xml方式輸出通信數(shù)據(jù)
* @param integer $code 狀態(tài)碼
* @param string $message 提示信息
* @param array $data 數(shù)據(jù)
* return string
*/
public static function xmlEncode($code, $message, $data = array()) {
if(!is_numeric($code)) {
return '';
}
$result = array(
'code' => $code,
'message' => $message,
'data' => $data,
);
header("Content-Type:text/xml");
$xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
$xml .= "<root>\n";
$xml .= self::xmlToEncode($result);
$xml .= "</root>";
echo $xml;
}
public static function xmlToEncode($data) {
$xml = $attr = "";
foreach($data as $key => $value) {
if(is_numeric($key)) {
$attr = " id='{$key}'";
$key = "item";
}
$xml .= "<{$key}{$attr}>";
$xml .= is_array($value) ? self::xmlToEncode($value) : $value;
$xml .= "</{$key}>\n";
}
return $xml;
}
}
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
- Thinkphp 在api開發(fā)中異常返回依然是html的解決方式
- thinkphp5框架API token身份驗(yàn)證功能示例
- ThinkPHP框架整合微信支付之JSAPI模式圖文詳解
- ThinkPHP實(shí)現(xiàn)微信支付(jsapi支付)流程教程詳解
- thinkPHP5.0框架API優(yōu)化后的友好性分析
- Thinkphp5框架ajax接口實(shí)現(xiàn)方法分析
- ThinkPHP框架實(shí)現(xiàn)的微信支付接口開發(fā)完整示例
- thinkPHP框架實(shí)現(xiàn)的短信接口驗(yàn)證碼功能示例
- thinkPHP微信分享接口JSSDK用法實(shí)例
- thinkPHP框架對(duì)接支付寶即時(shí)到賬接口回調(diào)操作示例
- ThinkPHP和UCenter接口沖突的解決方法
- thinkphp使用url請(qǐng)求調(diào)用ThinkApi天氣教程【圖文詳解】
相關(guān)文章
php數(shù)組轉(zhuǎn)換js數(shù)組操作及json_encode的用法詳解
php數(shù)組轉(zhuǎn)換js數(shù)組操作及json_encode的用法。需要的朋友可以過來(lái)參考下,希望對(duì)大家有所幫助2013-10-10
laravel框架 laravel-admin上傳圖片到oss的方法
今天小編就為大家分享一篇laravel框架 laravel-admin上傳圖片到oss的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-10-10
PHP與Ajax相結(jié)合實(shí)現(xiàn)登錄驗(yàn)證小Demo
AJAX即“Asynchronous Javascript And XML”(異步JavaScript和XML),是指一種創(chuàng)建交互式網(wǎng)頁(yè)應(yīng)用的網(wǎng)頁(yè)開發(fā)技術(shù)。接下來(lái)通過本文給大家分享PHP與Ajax相結(jié)合實(shí)現(xiàn)登錄驗(yàn)證小Demo,對(duì)php ajax實(shí)現(xiàn)登錄驗(yàn)證相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03
PHP實(shí)現(xiàn)數(shù)組向任意位置插入,刪除,替換數(shù)據(jù)操作示例
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)組向任意位置插入,刪除,替換數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了php中array_splice函數(shù)具體功能、參數(shù)及數(shù)組的插入、刪除、數(shù)值替換等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
php in_array() 檢查數(shù)組中是否存在某個(gè)值詳解
php有時(shí)候需要判斷某一個(gè)值是否存在于數(shù)組中,我們可以直接使用php內(nèi)置函數(shù)in_array()來(lái)實(shí)現(xiàn)判斷。php in_array 函數(shù)用于檢查數(shù)組中是否存在某個(gè)值,如果存在,則返回true,否則返回false。本文章向大家介紹in_array函數(shù)的基本語(yǔ)法及使用實(shí)例,感興趣的朋友可以參考一下。2016-11-11

