Simple example of thinkPHP5 framework interface writing
- 2021-12-13 07:43:48
- OfStack
In this paper, an example is given to describe the interface writing of thinkPHP5 framework. Share it for your reference, as follows:
Controller
/**
* Add shipping address
*/
public function addAddress(){
$post = $this->request->post();
// Validation Only 1 Rules: Table name, field name, excluding primary key value, primary key name
$validate = new \think\Validate([
['uid', 'require', ' Users id Cannot be empty '],
['name', 'require|max:20', ' Recipient cannot be empty '],
['mobile', 'require|length:11', ' Mobile phone number cannot be empty '],
['province_id', 'require', ' Province cannot be empty '],
['city_id', 'require', ' City cannot be empty '],
['district_id', 'require', ' County cannot be empty '],
['detail', 'require|max : 100', ' Address Details cannot be empty '],
],[
'mobile.length' => ' The mobile phone number format is incorrect ',
'name.max' => ' The recipient cannot exceed 20 Characters ',
'detail.max' => ' Address details cannot exceed 100 Characters ',
]);
// Verify the legitimacy of some data
if (!$validate->check($post)) {
\Org\Response::show(400,' Submission failed: ' . $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,' Add failed !');
}
}
model
<?php
namespace app\index\model;
use \think\Model;
use app\index\model\Attachment as AttachmentModel;
class Address extends Model
{
/**
* Get 1 Basic information
* @param int $id Administration 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 Users id
* @param $name Recipient
* @param $mobile Recipient's mobile phone number
* @param $province_id Provincial administration id
* @param $city_id City administration id
* @param $district_id County administration id
* @param $detail Detailed address
*/
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,' Invalid province !');
if(empty($is_city)) \Org\Response::show(400,' Invalid city !');
if(empty($is_district)) \Org\Response::show(400,' Invalid county !');
$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";
/**
* Output communication data in a comprehensive way
* @param integer $code Status code
* @param string $message Prompt information
* @param array $data Data
* @param string $type Data type
* 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
}
}
/**
* Press json Mode output communication data
* @param integer $code Status code
* @param string $message Prompt information
* @param array $data Data
* 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;
}
/**
* Press xml Mode output communication data
* @param integer $code Status code
* @param string $message Prompt information
* @param array $data Data
* 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;
}
}
Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to PHP programming based on ThinkPHP framework.