kirsankifat/request-data-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

kirsankifat/request-data-bundle

Composer 安装命令:

composer require kirsankifat/request-data-bundle

包简介

Represents request data in a structured and useful way.

README 文档

README

This bundle allows you to represent request data in a structured and useful way by creating request data classes.

Features:

  • Detecting how to extract data depends on request method and Content-Type header.
  • Representing and normalizing query parameters for the GET request method.
  • Representing form, json, xml request body for the POST, PUT, PATCH request methods.
  • Defining supported formats and throwing exception if the request format is unsupported.
  • Dispatching the finish event when request data is ready.

Installation

Run the following command using Composer:

composer require bilyiv/request-data-bundle

Configuration

The default configuration is the following:

request_data:
    prefix: App\RequestData

Usage

Create a request data class

namespace App\RequestData;

class PostRequestData implements FormatSupportableInterface
{
    public const DEFAULT_AUTHOR = 'none';

    /**
     * @var string
     */
    public $title;

    /**
     * @var string
     */
    public $author = self::DEFAULT_AUTHOR;

    /**
     * {@inheritdoc}
     */
    public static function getSupportedFormats(): array
    {
        return [Formats::FORM, Formats::JSON, Formats::XML];
    }
}

Use it in your controller

namespace App\Controller;

class PostController extends AbstractController
{
    /**
     * @Route("/", name="action")
     */
    public function action(PostRequestData $data)
    {
       return new JsonResponse($data);
    }
}

Make requests

All the following requests will return the same json response:

{
    "title": "Hamlet",
    "author": "William Shakespeare"
}

GET request:

curl -X GET 'https://example.com?title=Hamlet&author=William+Shakespeare'

POST form request:

curl -X POST 'https://example.com' \
     -H 'Content-Type: application/x-www-form-urlencoded' \
     -d 'title=Hamlet&author=William+Shakespeare'

POST json request:

curl -X POST 'https://example.com' \
     -H 'Content-Type: application/json' \
     -d '{"title":"Hamlet","author":"William Shakespeare"}'

POST xml request:

curl -X POST 'https://example.com' \
     -H 'Content-Type: application/xml' \
     -d '<post><title>Hamlet</title><author>William Shakespeare</author></post>'

POST csv request throws an exception because of unsupported format:

curl -X POST 'https://example.com' \
     -H 'Content-Type: application/csv' \
     -d 'Hamlet,William Shakespeare'

License

This bundle is released under the MIT license. See the included LICENSE file for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固