首頁  >  問答  >  主體

如何在Rest API操作中過濾/清理/驗證Symfony 5.4的請求參數

我對 Symfony 5.4 相當陌生,最近使用該版本創建了我的第一個 API

對於我的特定 API 端點,參數之一是 ID 陣列。

我需要透過以下方式驗證該數組:

我以簡單的方式實現了它,在使用類型轉換和現有的 Repository 持久化實體之前檢查數組:

$parentPropertyIds = (array)$request->request->get('parent_property_ids');
if ($parentPropertyIds) {
   $parentCount = $doctrine->getRepository(Property::class)->countByIds($parentPropertyIds);

   if ($parentCount !== count($parentPropertyIds)) {
       return $this->json([
            'status'  => 'error',
            'message' => 'parent_property_id_invalid'
       ], 422);
   }

   foreach ($parentPropertyIds as $parentPropertyId) {
      $parentProperty = $doctrine->getRepository(Property::class)->find($parentPropertyId);
      $property->addParent($parentProperty);
   }
}

但是,這使得我的控制器操作變得過於“身體積極”,並且感覺像是可以以更優雅的方式實現的東西。

我無法在 Symfony 5.4 文件中找到任何內容。

目前我想知道是否:

完整端點程式碼:

/**
     * @Route("/property", name="property_new", methods={"POST"})
     */
    public function create(ManagerRegistry $doctrine, Request $request, ValidatorInterface $validator): Response
    {
        $entityManager = $doctrine->getManager();

        $property = new Property();
        $property->setName($request->request->get('name'));
        $property->setCanBeShared((bool)$request->request->get('can_be_shared'));

        $parentPropertyIds = (array)$request->request->get('parent_property_ids');
        if ($parentPropertyIds) {
            $parentCount = $doctrine
                ->getRepository(Property::class)
                ->countByIds($parentPropertyIds);

            if ($parentCount !== count($parentPropertyIds)) {
                return $this->json([
                    'status'  => 'error',
                    'message' => 'parent_property_id_invalid'
                ], 422);
            }

            foreach ($parentPropertyIds as $parentPropertyId) {
                $parentProperty = $doctrine->getRepository(Property::class)->find($parentPropertyId);
                $property->addParent($parentProperty);
            }
        }

        $errors = $validator->validate($property);

        if (count($errors) > 0) {
            $messages = [];
            foreach ($errors as $violation) {
                $messages[$violation->getPropertyPath()][] = $violation->getMessage();
            }
            return $this->json([
                'status'   => 'error',
                'messages' => $messages
            ], 422);
        }

        $entityManager->persist($property);
        $entityManager->flush();

        return $this->json([
            'status' => 'ok',
            'id'     => $property->getId()
        ]);
    }

P粉617237727P粉617237727280 天前486

全部回覆(1)我來回復

  • P粉635509719

    P粉6355097192023-12-19 16:22:56

    您可以將資料傳輸物件 (DTO) 與 驗證服務。有許多預先定義約束,或者您可以建立一個自訂約束。 < /p>

    例如,如何使用簡單約束作為註解:

    class PropertyDTO {
      /**
       * @Assert\NotBlank
       */
      public string $name = "";
      public bool $shared = false;
    }

    然後將資料分配給DTO:

    $propertyData = new PropertyDTO();
    $propertyData->name = $request->request->get('name');
    ...

    在某些情況下,最好在 DTO 中定義建構函數,然後從 請求並立即傳遞給DTO:

    $data = $request->getContent(); // or $request->getArray(); depends on your content type
    $propertyData = new PropertyDTO($data);

    然後驗證它:

    $errors = $validator->validate($propertyData);
    
    if (count($errors) > 0) {
        /*
         * Uses a __toString method on the $errors variable which is a
         * ConstraintViolationList object. This gives us a nice string
         * for debugging.
         */
        $errorsString = (string) $errors;
    
        return $this->json([
                    'status'  => 'error',
                    'message' => 'parent_property_id_invalid'
                ], 422);
    }
    
    //...

    回覆
    0
  • 取消回覆