承接 mcc-api-tools/request-object-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mcc-api-tools/request-object-bundle

Composer 安装命令:

composer require mcc-api-tools/request-object-bundle

包简介

Simple RequestObject

README 文档

README

Request Object Bundle is a Symfony bundle designed to simplify handling HTTP requests by automatically transforming request parameters into objects and validating them.

Requirements

  • PHP 8.1 or higher (with php-json extension)
  • Symfony 6.2 or newer

Installation

Run the following command to install the package via Composer:

composer require mcc-api-tools/request-object-bundle

Configuration

  1. Add the package to your Symfony project (if not already added):

    // config/bundles.php
    
    return [
        // ...
        MccApiTools\RequestObjectBundle\RequestObjectBundle::class => ['all' => true],
    ];
  2. Create a request object that will represent your HTTP request data:

    // src/Request/CreateLocationRequest.php
    
    declare(strict_types=1);
    
    namespace App\Request;
    
    use MccApiTools\RequestObjectBundle\Model\AllowExtraAttributesInterface;
    use MccApiTools\RequestObjectBundle\Model\RequestObjectInterface;
    use Symfony\Component\Validator\Constraints as Assert;
    
    class CreateLocationRequest implements RequestObjectInterface, AllowExtraAttributesInterface
    {
        #[Assert\NotBlank(message: "The location name is required")]
        public string $name;
    
        #[Assert\NotBlank(message: "The location address is required")]
        public string $address;
    
        #[Assert\NotBlank(message: "The latitude is required")]
        #[Assert\Type(type: "float", message: "The latitude must be a number")]
        public float $latitude;
    
        #[Assert\NotBlank(message: "The longitude is required")]
        #[Assert\Type(type: "float", message: "The longitude must be a number")]
        public float $longitude;
    }
  3. Use the request object in your controller:

    // src/Controller/LocationController.php
    
    declare(strict_types=1);
    
    namespace App\Controller;
    
    use App\Request\CreateLocationRequest;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Attribute\Route;
    
    class LocationController extends AbstractController
    {
        #[Route("/locations", methods: ["POST"])]
        public function create(CreateLocationRequest $request): Response
        {
            // Access request data via $request->name, $request->address, $request->latitude, $request->longitude
    
            // Logic for creating a location
    
            return new Response(null, 204);
        }
    }

Usage

  • Validation: Request objects are automatically validated based on the constraints defined in their properties. If validation fails, a response with validation errors will be returned.

  • Accessing Data: You can access request data directly through the request object properties in your controller.

Testing

To run tests, use the following command:

composer test

Contributing

If you would like to contribute to this package, please open an issue or submit a pull request in the GitHub repository.

License

This project is licensed under the MIT License

统计信息

  • 总下载量: 2.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固