2015-12-16 120 views
9

我一直在開發一組其餘的API以供移動應用程序使用。我正在遵循Laravel項目開發的存儲庫模式。我如何實現一個演示者和轉換器,用於在我的所有API中設置一個常量JSON輸出的格式?Laravel + Dingo中一致的REST API響應

例如我爲登錄

public function authenticate() 
    { 
     $request = Request::all(); 
     try { 
       // If authenticated, issue JWT token 
       //Showing a dummy response 
       return $token; 
      } catch (ValidatorException $e) { 
       return Response::json([ 
        'error' =>true, 
        'message' =>$e->getMessageBag() 
       ]); 
      } 
    } 

現在以下控制器哪裏變壓器和主持人進入了嗎?我知道通過轉換數據庫對象來格式化輸出,並生成格式化的JSON,以便在我的API中保持統一。

遊戲的API和分形甚至框架(L5 repository)沒有提供詳細的文檔,我找不到任何教程。

我已經創造了另一個 API賦予產品

namespace App\Api\V1\Transformers; 

use App\Entities\Product; 
use League\Fractal\TransformerAbstract; 

class UserTransformer extends TransformerAbstract { 

    public function transform(\Product $product) 
    { 
     return [ 
      'id'  => (int) $product->products_id 
     ]; 
    } 
} 

列表演示

<?php 

namespace App\Api\V1\Presenters; 

use App\Api\V1\Transformers\ProductTransformer; 
use Prettus\Repository\Presenter\FractalPresenter; 

/** 
* Class ProductPresenter 
* 
* @package namespace App\Presenters; 
*/ 
class ProductPresenter extends FractalPresenter 
{ 
    /** 
    * Transformer 
    * 
    * @return \League\Fractal\TransformerAbstract 
    */ 
    public function getTransformer() 
    { 
     return new UserTransformer(); 
    } 
} 

我將如何設置演示控制器和背部應對以下主持人和變壓器?試過

$this->repository->setPresenter("App\\Presenter\\PostPresenter"); 

但它似乎沒有工作,文檔並沒有顯示完整的步驟。

  1. 在上面的例子中,我該如何爲我可以在我的API中使用的錯誤響應制作模板,以及如何將錯誤例外傳遞給它?
  2. 看起來好像演示者和變換器可以用來將數據庫對象轉換爲可以表現的JSON,而不是其他任何東西。是對的嗎?
  3. 您如何使用演示者和轉換器來獲得成功響應和錯誤響應?通過傳遞異常,而不是DB對象到變換器?

回答

0

分形是完全記錄在這裏:http://fractal.thephpleague.com/ 有,我經常從菲爾鱘魚讀https://leanpub.com/build-apis-you-wont-hate你可以發現,大部分的在github上https://github.com/philsturgeon/build-apis-you-wont-hate提供的書籍代碼的優秀圖書。你可以在那裏找到非常好的分形的例子。

  1. 我會創造一個API控制器和從我Controllers.In擴展它有應該有所有的響應函數(respondWithError,respondWithArray等)
  2. 以一致的JSON格式,以便
  3. 發電產品的變換對象所有的端點都會爲每個實體返回相同的結果。

  4. 真的沒有這個

  5. 答案還有分形文檔中足夠的例子。
1

我有完全相同的問題,這裏是我用野狗帶變壓器

控制器:

public function update(Request $request) 
{ 

    $bus = new CommandBus([ 
     $this->commandHandlerMiddleware 
    ]); 

    $agency = $bus->handle(
     new UpdateAgencyCommand($request->user()->getId(), $request->route('id'), $request->only('name')) 
    ); 

    return $this->response->item($agency, new AgencyTransformer()); 
} 

變壓器:

class AgencyTransformer extends TransformerAbstract 
{ 
    public function transform(AgencyEntity $agencyEntity) 
    { 
     return [ 
      'id' => (int) $agencyEntity->getId(), 
      'name' => $agencyEntity->getName(), 
     ]; 
    } 
} 

,這是我如何處理錯誤:

throw new UpdateResourceFailedException('Could not update agency.', $this->agencyUpdateValidator->errors()); 
1

我剛纔在這裏也看到了類似的問題。所以請在這裏看到我對你的其他問題的回答:https://stackoverflow.com/a/34430595/429719

從我得到的您正在使用Dingo其他問題,所以使用它作爲一個結構化的響應類。請確保你控制器從Dingo延伸,那麼你可以只返回一個結構化的方式的項目和收藏,如:

return $this->response->item($user, new UserTransformer);

return $this->response->collection($users, new UserTransformer);

如果你想要一個漂亮的錯誤處理在這裏看的文檔:https://github.com/dingo/api/wiki/Errors-And-Error-Responses

基本上可以拋出任何核心異常或幾個自定義的Dingo。 Dingo圖層會捕獲它們並返回結構化的JSON響應。 按巴丁格文檔:

throw new Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException('Nope, no entry today!');

會生成:

{ 
    "message": "Nope, no entry today!", 
    "status_code": 403 
} 
+0

回報$這個 - >響應 - >項目($用戶,新UserTransformer);實際上並沒有改變我的迴應。沒有錯誤或其中顯示任何內容。我只是得到沒有任何轉換的普通json。 – Ajeesh