簡體   English   中英

Symfony4-在靜態API中處理有效負載的位置

[英]Symfony4 - Where process the payload in restful api

我一直在研究一個寧靜的api,我想知道驗證后應該在哪里處理有效負載。 現在看起來像這樣:

// src/Controller/ExampleController.php
public function create(Request $request, EntityManagerInterface $manager)
{
    // the getPayload gonna return a object representing my data
    // already validate, ready to be processed
    $data = $this->getPayload()

    $exampleEntity = new ExampleEntity();
    $exampleEntity
        ->setUser($this->getUser())
        ->setBook($data->book);
    $manager->persist($exampleEntity);
    $manager->flush();

    return new JsonResponse($example->getResult());
}

建議我創建一個服務來處理數據,不要在控制器中直接使用。 我實際上想在控制器外部分離數據,但是我應該為每個控制器創建一個服務嗎? 像這樣的東西:

// src/Controller/SomeController.php
public function create(Request $request, \App\Service\Example $example)
{
    // the getPayload gonna return a object representing my data
    // already validate, ready to be processed
    $data = $this->getPayload()
    $example->setPayload($data);
    $example->process();

    return new JsonResponse($example->getResult());
}

我還有其他問題,是否應該驗證標識符,例如,如果我收到傳遞書ID的json正文,是否創建新約束以驗證書是否存在,則需要在數據庫中進行查詢(因為我是在有效負載到達控制器之前自動驗證有效負載),然后再進行一次查詢以實際創建關系。 例:

// src/Controller/ExampleController.php
public function create(
    Request $request,
    ExampleReposiry $repository,
    EntityManagerInterface $manager)
{
    // the getPayload gonna return a object representing my data
    // already validate, ready to be processed
    $data = $this->getPayload()

    $exampleEntity = new ExampleEntity();
    $exampleEntity
        ->setUser($this->getUser())
        // $data->book is only the id, not the actually object book
        // this is the second time query for the object, the fist
        // time was inside the custom constraint that validate
        // to see if the id pass is valid.
        ->setBook($repository->findBy(['id' => $data->book]));
    $manager->persist($exampleEntity);
    $manager->flush();

    return new JsonResponse($example->getResult());
}

或者,我只是假設圖書通行證的ID有效,否則我就拋出異常?

我跟隨以自動驗證此處數據的帖子

建議我創建一個服務來處理數據,不要在控制器中直接使用。 我實際上想在控制器外部分離數據,但是我應該為每個控制器創建一個服務嗎?

或者,我只是假設圖書通行證的ID有效,否則我就拋出異常?

我將職責划分如下。

  1. 創建一個ExampleService類(發生業務邏輯的地方)並將其注入到ExampleController
  2. 將經過驗證的數據集傳遞給ExampleService類。
  3. 檢查書籍ID是否在DB中。 否則拋出異常。
  4. 創建一個ExampleFactory類(在其中進行數據映射)並將其注入到ExampleService
  5. 將您的數據集傳遞給ExampleFactory以便它返回ExampleEntity實例。
  6. ExampleEntity實例上調用persistflush 假設您已經將ExampleRepository類注入到ExampleService類中。

然后執行您需要做的其他事情,並在ExampleController返回Response::HTTP_CREATED ExampleController

這是我基於@BentCoder回答的實現。 經過一天的工作后想到工廠還是沒有工廠? 我最終嘗試使用工廠模式,因為:

  • 遵循經驗更豐富的程序員的明智建議(或至少嘗試一下)
  • 更好地理解模式
  • 嘗試新事物(這是一個附帶項目,所以可以把它搞砸了)

     // src/Controller/ExampleController.php public function create(\\App\\Service\\Example $exampleService) { $exampleService->process($this->getPayload()); // .. do something } // src/Service/Example.php public function process(App\\Model\\ExampleInterface $data) { // factory was receive via dependency injection in the constructor $objects = $this->factory->create($data); // .. do something } // src/Model/Example/Create.php class Create implements App\\Model\\ExampleInterface { public $property1; public $property2; } // src/Model/Example/Update.php class Update implements App\\Model\\ExampleInterface { public $property2; } // src/Factory/ExampleFactory.php public function create(App\\Model\\ExampleInterface $data) { if ($data instanceof App\\Model\\Example\\Create::class) ( // .. return a array of instantiated object } elseif ($data instanceof App\\Model\\Example\\Update::class) { // .. returm only one object } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM