承接 elao/json-http-form-bundle 相关项目开发

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

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

elao/json-http-form-bundle

最新稳定版本:v3.1.0

Composer 安装命令:

composer require elao/json-http-form-bundle

包简介

Adds support of JSON requests for Forms

README 文档

README

Build Status

Adds support of JSON requests for Forms:

Symfony forms will be able to handle both JSON POST/PUT/PATCH/DELETE requests and standard GET/POST requests (as they are by default).

The JsonHttpFoundationRequestHandler handles the request: If the request content-type is JSON, it decodes the JSON request content as an array and submits the form with its data.

Otherwise, it lets the default behaviour operate: the HttpFoundationRequestHandler will handle the request. So all your non-json form request will be treated just the way they've always been.

Installation

Require ElaoJsonHttpFormBundle:

composer require elao/json-http-form-bundle

Usage

Given a Rocket entity with two attributes: name (a string) and colors (an array of strings).

The following form and controller are meant to create a new instance of Rocket:

<?php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

// ...

class RocketType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class)
            ->add('colors', ChoiceType::class, [
                'multiple' => true,
                'choices'  => [
                    'White'  => 'white',
                    'Orange' => 'orange',
                    'Blonde' => 'blonde',
                    'Pink'   => 'pink',
                    'Blue'   => 'blue',
                    'Brown'  => 'brown',
                ]
            ])
        ;
    }

    // ...
}
<?php

namespace AppBundle\Controller;

// ...

class RocketController extends Controller
{
    public function newAction(Request $request)
    {
        $rocket = new Rocket();
        $form   = $this->createForm(new RocketType(), $rocket)->getForm();

        if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
            // The $rocket object is now correctly hydrated with the data from the form.
            // Whether the request is a classic GET/POST request or a JSON one.
        }
    }
}

The Controller and Form above now accept the following JSON POST request:

POST /rockets HTTP/1.1
Accept: application/json
Content-Type: application/json
Content-Length: 43

{"name":"Melies","colors":["pink","brown"]}

It works \o/

License

MIT

Author Information

http://www.elao.com/

统计信息

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

GitHub 信息

  • Stars: 35
  • Watchers: 15
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固