Laravel5.x如何让所有的错误都返回一个json?

WBOY
Release: 2016-06-06 20:28:09
Original
837 people have browsed it

比如正常的一个请求,返回方式如下:

<code class="json">return response($result, $code);  // 返回json</code>
Copy after login
Copy after login

api接口返回html不方便看,怎么让任何错误返回json,包括代码错误,数据库连接错误

回复内容:

比如正常的一个请求,返回方式如下:

<code class="json">return response($result, $code);  // 返回json</code>
Copy after login
Copy after login

api接口返回html不方便看,怎么让任何错误返回json,包括代码错误,数据库连接错误

App\Exceptions\Handler 的render方法里,直接return response()->json();

这里面你可以对不同的错误返回不同的json

这里

<code class="php">try{ 
    $conn = new redis();
 }catch (\Exception $e){ 
    var_dump($e);//捕获错误
}</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template