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

Thinkphp 在api開(kāi)發(fā)中異常返回依然是html的解決方式

 更新時(shí)間:2019年10月16日 11:20:11   作者:碼痘痘  
今天小編就為大家整理了一篇Thinkphp 在api開(kāi)發(fā)中異常返回依然是html的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

現(xiàn)在誰(shuí)不開(kāi)發(fā)接口的呢?但是在接口開(kāi)發(fā)過(guò)程中,報(bào)錯(cuò)誤異常后居然返回錯(cuò)誤的信息依然是html信息!TP官方也不知道為啥不添加,說(shuō)好的為接口而生,我的解決方案也很簡(jiǎn)單,把系統(tǒng)的異常處理類復(fù)制出來(lái),去掉模板相關(guān),直接以json方式輸出

下面是解決方案:

1:按照TP擴(kuò)展異常的方式引用這個(gè)文件

https://www.kancloud.cn/manual/thinkphp5_1/354092

// 判斷默認(rèn)輸出類型
// $app 是配置數(shù)組
if ($app['default_return_type'] == 'json') {
 // 異常處理handle類 留空使用 \think\exception\Handle
 $app['exception_handle'] = '\\app\\common\\exception\\JsonException';
}
return $app;

異常處理類:

<?php

 namespace app\common\exception;


 use Exception;
 use think\exception\ErrorException;
 use think\exception\Handle;
 use think\exception\HttpException;
 use think\console\Output;
 use think\Container;
 use think\Response;


 class JsonException extends Handle
 {
  protected $render;
  protected $ignoreReport = [
   '\\think\\exception\\HttpException',
  ];

  public function setRender($render)
  {
   $this->render = $render;
  }

  /**
  * Report or log an exception.
  *
  * @access public
  * @param \Exception $exception
  * @return void
  */
  public function report(Exception $exception)
  {
   if (!$this->isIgnoreReport($exception)) {
   // 收集異常數(shù)據(jù)
   if (Container::get('app')->isDebug()) {
    $data = [
     'file' => $exception->getFile(),
     'line' => $exception->getLine(),
     'message' => $this->getMessage($exception),
     'code' => $this->getCode($exception),
    ];
    $log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
   } else {
    $data = [
     'code' => $this->getCode($exception),
     'message' => $this->getMessage($exception),
    ];
    $log = "[{$data['code']}]{$data['message']}";
   }

   if (Container::get('app')->config('log.record_trace')) {
    $log .= "\r\n" . $exception->getTraceAsString();
   }

   Container::get('log')->record($log, 'error');
   }
  }

  protected function isIgnoreReport(Exception $exception)
  {
   foreach ($this->ignoreReport as $class) {
   if ($exception instanceof $class) {
    return true;
   }
   }

   return false;
  }

  /**
  * Render an exception into an HTTP response.
  *
  * @access public
  * @param \Exception $e
  * @return Response
  */
  public function render(Exception $e)
  {
   if ($this->render && $this->render instanceof \Closure) {
   $result = call_user_func_array($this->render, [$e]);

   if ($result) {
    return $result;
   }
   }

   if ($e instanceof HttpException) {
   return $this->renderHttpException($e);
   } else {
   return $this->convertExceptionToResponse($e);
   }
  }

  /**
  * @access public
  * @param Output $output
  * @param Exception $e
  */
  public function renderForConsole(Output $output, Exception $e)
  {
   if (Container::get('app')->isDebug()) {
   $output->setVerbosity(Output::VERBOSITY_DEBUG);
   }

   $output->renderException($e);
  }

  /**
  * @access protected
  * @param HttpException $e
  * @return Response
  */
  protected function renderHttpException(HttpException $e)
  {
   $status = $e->getStatusCode();
   $template = Container::get('app')->config('http_exception_template');

   if (!Container::get('app')->isDebug() && !empty($template[$status])) {
   return Response::create($e, 'json', $status);
   } else {
   return $this->convertExceptionToResponse($e);
   }
  }

  /**
  * @access protected
  * @param Exception $exception
  * @return Response
  */
  protected function convertExceptionToResponse(Exception $exception)
  {
   // 收集異常數(shù)據(jù)
   if (Container::get('app')->isDebug()) {
   // 調(diào)試模式,獲取詳細(xì)的錯(cuò)誤信息
   $data = [
    'name' => get_class($exception),
    'file' => $exception->getFile(),
    'line' => $exception->getLine(),
    'message' => $this->getMessage($exception),
    'trace' => $exception->getTrace(),
    'code' => $this->getCode($exception),
    'source' => $this->getSourceCode($exception),
    'datas' => $this->getExtendData($exception),
    'tables' => [
     'GET Data'    => $_GET,
     'POST Data'    => $_POST,
     'Files'     => $_FILES,
     'Cookies'    => $_COOKIE,
     'Session'    => isset($_SESSION) ? $_SESSION : [],
     'Server/Request Data' => $_SERVER,
     'Environment Variables' => $_ENV,
     'ThinkPHP Constants' => $this->getConst(),
    ],
   ];
   } else {
   // 部署模式僅顯示 Code 和 Message
   $data = [
    'code' => $this->getCode($exception),
    'message' => $this->getMessage($exception),
   ];

   if (!Container::get('app')->config('show_error_msg')) {
    // 不顯示詳細(xì)錯(cuò)誤信息
    $data['message'] = Container::get('app')->config('error_message');
   }
   }

   //保留一層
   while (ob_get_level() > 1) {
   ob_end_clean();
   }

   $data['echo'] = ob_get_clean();

   $response = Response::create($data, 'json');

   if ($exception instanceof HttpException) {
   $statusCode = $exception->getStatusCode();
   $response->header($exception->getHeaders());
   }

   if (!isset($statusCode)) {
   $statusCode = 500;
   }
   $response->code($statusCode);

   return $response;
  }

  /**
  * 獲取錯(cuò)誤編碼
  * ErrorException則使用錯(cuò)誤級(jí)別作為錯(cuò)誤編碼
  * @access protected
  * @param \Exception $exception
  * @return integer    錯(cuò)誤編碼
  */
  protected function getCode(Exception $exception)
  {
   $code = $exception->getCode();

   if (!$code && $exception instanceof ErrorException) {
   $code = $exception->getSeverity();
   }

   return $code;
  }

  /**
  * 獲取錯(cuò)誤信息
  * ErrorException則使用錯(cuò)誤級(jí)別作為錯(cuò)誤編碼
  * @access protected
  * @param \Exception $exception
  * @return string    錯(cuò)誤信息
  */
  protected function getMessage(Exception $exception)
  {
   $message = $exception->getMessage();

   if (PHP_SAPI == 'cli') {
   return $message;
   }

   $lang = Container::get('lang');

   if (strpos($message, ':')) {
   $name = strstr($message, ':', true);
   $message = $lang->has($name) ? $lang->get($name) . strstr($message, ':') : $message;
   } elseif (strpos($message, ',')) {
   $name = strstr($message, ',', true);
   $message = $lang->has($name) ? $lang->get($name) . ':' . substr(strstr($message, ','), 1) : $message;
   } elseif ($lang->has($message)) {
   $message = $lang->get($message);
   }

   return $message;
  }

  /**
  * 獲取出錯(cuò)文件內(nèi)容
  * 獲取錯(cuò)誤的前9行和后9行
  * @access protected
  * @param \Exception $exception
  * @return array     錯(cuò)誤文件內(nèi)容
  */
  protected function getSourceCode(Exception $exception)
  {
   // 讀取前9行和后9行
   $line = $exception->getLine();
   $first = ($line - 9 > 0) ? $line - 9 : 1;

   try {
   $contents = file($exception->getFile());
   $source = [
    'first' => $first,
    'source' => array_slice($contents, $first - 1, 19),
   ];
   } catch (Exception $e) {
   $source = [];
   }

   return $source;
  }

  /**
  * 獲取異常擴(kuò)展信息
  * 用于非調(diào)試模式html返回類型顯示
  * @access protected
  * @param \Exception $exception
  * @return array     異常類定義的擴(kuò)展數(shù)據(jù)
  */
  protected function getExtendData(Exception $exception)
  {
   $data = [];

   if ($exception instanceof \think\Exception) {
   $data = $exception->getData();
   }

   return $data;
  }

  /**
  * 獲取常量列表
  * @access private
  * @return array 常量列表
  */
  private static function getConst()
  {
   $const = get_defined_constants(true);

   return isset($const['user']) ? $const['user'] : [];
  }

 }

以上這篇Thinkphp 在api開(kāi)發(fā)中異常返回依然是html的解決方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • php 讀取輸出其他文件的實(shí)現(xiàn)方法

    php 讀取輸出其他文件的實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇php 讀取輸出其他文件的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-07-07
  • php實(shí)現(xiàn)數(shù)組篩選奇數(shù)和偶數(shù)示例

    php實(shí)現(xiàn)數(shù)組篩選奇數(shù)和偶數(shù)示例

    這篇文章主要介紹了php實(shí)現(xiàn)數(shù)組篩選奇數(shù)和偶數(shù)示例,需要的朋友可以參考下
    2014-04-04
  • ThinkPHP里用U方法調(diào)用js文件實(shí)例

    ThinkPHP里用U方法調(diào)用js文件實(shí)例

    這篇文章主要介紹了ThinkPHP里用U方法調(diào)用js文件的方法,實(shí)例分析了ThinkPHP中U方法的使用技巧,需要的朋友可以參考下
    2015-06-06
  • php教程之魔術(shù)方法的使用示例(php魔術(shù)函數(shù))

    php教程之魔術(shù)方法的使用示例(php魔術(shù)函數(shù))

    這篇文章主要介紹了php的魔術(shù)方法的使用示例(php魔術(shù)函數(shù)),需要的朋友可以參考下
    2014-02-02
  • php檢測(cè)iis環(huán)境是否支持htaccess的方法

    php檢測(cè)iis環(huán)境是否支持htaccess的方法

    這篇文章主要介紹了php檢測(cè)iis環(huán)境是否支持htaccess的方法,需要的朋友可以參考下
    2014-02-02
  • PHP curl模擬登錄帶驗(yàn)證碼的網(wǎng)站

    PHP curl模擬登錄帶驗(yàn)證碼的網(wǎng)站

    最近接了個(gè)項(xiàng)目,其中有需求是要登錄帶驗(yàn)證碼的網(wǎng)站,獲取數(shù)據(jù),但是我們不可能人為的一直去記錄數(shù)據(jù),想通過(guò)自動(dòng)采集的方式進(jìn)行,下面小編給大家?guī)?lái)的相關(guān)代碼,對(duì)php curl 模擬登錄帶驗(yàn)證碼的網(wǎng)站感興趣的朋友一起學(xué)習(xí)吧
    2015-11-11
  • Yii CGridView用法實(shí)例詳解

    Yii CGridView用法實(shí)例詳解

    這篇文章主要介紹了Yii CGridView用法,結(jié)合實(shí)例形式分析了CGridView的功能、用法與相關(guān)屬性用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • phpstudy2020搭建站點(diǎn)的實(shí)現(xiàn)示例

    phpstudy2020搭建站點(diǎn)的實(shí)現(xiàn)示例

    這篇文章主要介紹了phpstudy2020搭建站點(diǎn)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 使用php顯示搜索引擎來(lái)的關(guān)鍵詞

    使用php顯示搜索引擎來(lái)的關(guān)鍵詞

    在訪客從搜索引擎而來(lái)的第一個(gè)頁(yè)面上顯示訪客搜索的關(guān)鍵詞,根據(jù)這個(gè)關(guān)鍵詞做出一些提高網(wǎng)站交互能力的改變,比如顯示這個(gè)關(guān)鍵詞相關(guān)的其它文章
    2014-02-02
  • 詳解PHP中websocket的使用方法

    詳解PHP中websocket的使用方法

    這篇文章主要為大家詳細(xì)介紹了PHP中websocket的使用方法,為大家進(jìn)行了詳細(xì)注解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09

最新評(píng)論