欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ThinkPHP?通用的API格式封裝實(shí)例代碼

 更新時(shí)間:2023年08月29日 14:22:28   作者:殺死一只知更鳥debug  
這篇文章主要介紹了ThinkPHP通用的API格式封裝,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

ThinkPHP 通用的API格式封裝

1.創(chuàng)建status.php 用于設(shè)置通用的狀態(tài)碼返回枚舉類

<?php
return[
    "success"=>1,
    "error"=>0,
    "controller_not_found"=>-1,
    "action_not_found"=>-2,
];

2.將API返回格式統(tǒng)一封裝

<?php
/**API 統(tǒng)一格式化
 * @param $status
 * @param $message
 * @param $data
 * @param $httpStatus
 * @return \think\response\Json
 */
function show($status,$message,$data=[],$httpStatus=200){
    $result = [
      "status"=>$status,
      "message"=>$message,
      "data"=>$data,
    ];
    return json($result,$httpStatus);
}

3.重寫B(tài)aseController中的__call方法

在BaseController中重寫__call方法,當(dāng)調(diào)用不存在的方法時(shí),就會(huì)調(diào)用__call方法,會(huì)傳入請求方法和參數(shù)

<?php
declare (strict_types = 1);
namespace app;
use think\App;
use think\exception\ValidateException;
use think\Validate;
/**
 * 控制器基礎(chǔ)類
 */
abstract class BaseController
{
    /**
     * Request實(shí)例
     * @var \think\Request
     */
    protected $request;
    /**
     * 應(yīng)用實(shí)例
     * @var \think\App
     */
    protected $app;
    /**
     * 是否批量驗(yàn)證
     * @var bool
     */
    protected $batchValidate = false;
    /**
     * 控制器中間件
     * @var array
     */
    protected $middleware = [];
    /**
     * 構(gòu)造方法
     * @access public
     * @param  App  $app  應(yīng)用對象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;
        // 控制器初始化
        $this->initialize();
    }
    // 初始化
    protected function initialize()
    {}
    /**
     * 驗(yàn)證數(shù)據(jù)
     * @access protected
     * @param  array        $data     數(shù)據(jù)
     * @param  string|array $validate 驗(yàn)證器名或者驗(yàn)證規(guī)則數(shù)組
     * @param  array        $message  提示信息
     * @param  bool         $batch    是否批量驗(yàn)證
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, string|array $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持場景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v     = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }
        $v->message($message);
        // 是否批量驗(yàn)證
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }
        return $v->failException(true)->check($data);
    }
	// 重寫__call部分
    public function __call(string $name, array $arguments)
    {
        // TODO: Implement __call() method.
        return show(config("status.action_not_found"),"找不到{$name}方法",null,404);
    }
}

在這里插入圖片描述

4.在控制器下面新建Error控制器,然后添加__call方法

在控制器下面新建Error控制器,然后添加__call方法,這樣就會(huì)調(diào)用不存在的控制器的時(shí)候會(huì)調(diào)用__call方法,會(huì)傳入請求的控制器名稱和參數(shù)

<?php
namespace app\index\controller;
class Error{
    public function __call(string $name, array $arguments)
    {
        // TODO: Implement __call() method.
        return show(config("status.controller_not_found"),"找不到{$name}控制器",null,404);
    }
}

測試

在這里插入圖片描述

在這里插入圖片描述

假定一個(gè)成功的請求,測試一下

public function jsonTest(){
    $data = ['name' => 'thinkphp', 'status' => '1'];
    return show(config("status.success"),"請求成功",$data);
}

在這里插入圖片描述

到此這篇關(guān)于ThinkPHP 通用的API格式封裝的文章就介紹到這了,更多相關(guān)ThinkPHP  API格式封裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論