久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      讓 Laravel API 永遠(yuǎn)返回 JSON 格式響應(yīng)

      讓 Laravel API 永遠(yuǎn)返回 JSON 格式響應(yīng)

      當(dāng)你在編寫(xiě)完全為 API 服務(wù)的 Laravel 應(yīng)用時(shí),你希望所有響應(yīng)都是 JSON 格式的,而不是例如說(shuō)授權(quán)錯(cuò)誤會(huì)重定向到 /home 或 /login,最終重定向會(huì)變成 InvalidArgumentException: Route [login] is not defined. 的視圖。

      推薦教程:《laravel教程》

      下面這個(gè)簡(jiǎn)單的方案,可以讓你的 Laravel 應(yīng)用優(yōu)先響應(yīng)為 JSON 格式。

      第一步、編寫(xiě) BaseRequest

      首先我們需要構(gòu)建一個(gè) BaseRequest 來(lái)重寫(xiě) IlluminateHttpRequest ,修改為默認(rèn)優(yōu)先使用 JSON 響應(yīng):

      app/Http/Requests/BaseRequest.php

      <?php namespace AppHttpRequests; use IlluminateHttpRequest; class BaseRequest extends Request {     public function expectsJson()     {         return true;     }     public function wantsJson()     {         return true;     } }

      第二步、替換 BaseRequest

      在 public/index.php 文件中,將 IllumiateHttpRequest 替換為我們的 BaseRequest,如下:

      $response = $kernel->handle(     $request = AppHttpRequestsBaseRequest::capture() );

      搞定!

      現(xiàn)在所有的響應(yīng)都是 application/json ,包括錯(cuò)誤和異常。

      來(lái)自于社區(qū) https://learnku.com/laravel/wikis/16069

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)