chaplean/form-handler-bundle 问题修复 & 功能扩展

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

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

chaplean/form-handler-bundle

Composer 安装命令:

composer require chaplean/form-handler-bundle

包简介

The Bundle base to fork

README 文档

README

1. Composer

composer require chaplean/form-handler-bundle

2. AppKernel.php

Add

            new Chaplean\Bundle\FormHandlerBundle\ChapleanFormHandlerBundle(),

Usage

Bellow are few fake controller actions to show of what you can do.

<?php

namespace App\Bundle\RestBundle\Controller;

use FOS\RestBundle\Controller\Annotations;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;

/**
 * @Annotations\RouteResource("DummyEntity")
 */
class FakeController extends FOSRestController
{
    /*
     * Those two first actions uses the defaults behaviours of FormHandler.
     * On success it will persist the DummyEntity given to handle.
     * On failure it will convert the form errors to an angular friendly format.
     * No groups will be used when serializing your entity to create the response on success.
     *
     * The first action works with a new DummyEntity.
     * The second action works with an existing DummyEntity.
     */

    public function postAction(Request $request)
    {
        return $this
            ->get('chaplean_form_handler.form_handler')
            ->handle(DummyEntityType::class, new DummyEntity(), $request);
    }

    public function putAction(Request $request, DummyEntity $dummy)
    {
        return $this
            ->get('chaplean_form_handler.form_handler')
            ->handle(DummyEntityType::class, $dummy, $request);
    }

    /*
     * This action defines groups to use to serialize the DummyEntity to create the response on success.
     */
    public function postWithGroupsAction(Request $request)
    {
        return $this
            ->get('chaplean_form_handler.form_handler')
            ->setGroups(['dummy_entity_name'])
            ->handle(DummyEntityType::class, new DummyEntity(), $request);
    }

    /*
     * The SuccessHandler is the logic that's run when the form submission is valid.
     * The default behaviour is to persist the entity.
     * With the successHandler function you can override this default behaviour by giving an
     * alternative implementation of SuccessHandlerInterface.
     * 
     * successHandler takes a string that's the name of the class implementing SuccessHandlerInterface
     * you registered in the container under.
     * 
     * If the class you gave happens to also implement PreprocessorInterface and / or ValidatorInterface
     * and you haven't called the preprocessor and / or validator methods, your class will be used
     * as the preprocessor and / or validator.
     */
    public function postWithCustomSuccessHandler(request $request)
    {
        return $this
            ->get('chaplean_form_handler.form_handler')
            ->successHandler('your_bundle.form.success_handler.do_something_on_valid_form')
            ->handle(DummyEntityType::class, new DummyEntity(), $request);
    }

    /*
     * The FailureHandler is the logic that's run when the form submission is invalid.
     * The default behaviour is to transform the form errors into an angular friendly format.
     * With the failureHandler function you can override this default behaviour by giving an
     * alternative implementation of FailureHandlerInterface.
     * 
     * failureHandler takes a string that's the name of the class implementing FailureHandlerInterface
     * you registered in the container under.
     */
    public function postWithCustomFailureHandler(request $request)
    {
        return $this
            ->get('chaplean_form_handler.form_handler')
            ->failureHandler('your_bundle.form.failure_handler.do_something_on_invalid_form')
            ->handle(DummyEntityType::class, new DummyEntity(), $request);
    }

    /*
     * The Preprocessor is the logic that's run before the form validation logic is run.
     * The goal of this is to allow you to run custom transformation (like maybe some filters, or coversions)
     * on the data to submit before it is submited.
     * With the preprocessor function you can define the behaviour you want by giving an
     * implementation of PreprocessorInterface.
     * 
     * preprocessor takes a string that's the name of the class implementing PreprocessorInterface
     * you registered in the container under.
     */
    public function postWithCustomPreprocessor(request $request)
    {
        return $this
            ->get('chaplean_form_handler.form_handler')
            ->preprocessor('your_bundle.form.preprocessor.do_custom_preprocessing_on_data')
            ->handle(DummyEntityType::class, new DummyEntity(), $request);
    }

    /*
     * The Validator allows you to define custom validation logic outside of symfony form validator.
     * With the preprocessor function you can define the behaviour you want by giving an
     * implementation of ValidatorInterface.
     * 
     * preprocessor takes a string that's the name of the class implementing ValidatorInterface
     * you registered in the container under.
     */
    public function postWithCustomValidator(request $request)
    {
        return $this
            ->get('chaplean_form_handler.form_handler')
            ->validator('your_bundle.form.validator.do_custom_data_validation')
            ->handle(DummyEntityType::class, new DummyEntity(), $request);
    }

    /*
     * This last action uses the whole api capabilities just for the sake of showing it off.
     */
    public function postWithFullApiUsageAction(request $request)
    {
        return $this
            ->get('chaplean_form_handler.form_handler')
            ->successHandler('your_bundle.form.success_handler.do_something_on_valid_form')
            ->failureHandler('your_bundle.form.failure_handler.do_something_on_invalid_form')
            ->preprocessor('your_bundle.form.preprocessor.do_custom_preprocessing_on_data')
            ->validator('your_bundle.form.validator.do_custom_data_validation')
            ->setGroups(['dummy_entity_name'])
            ->handle(DummyEntityType::class, new DummyEntity(), $request);
    }
}

chaplean/form-handler-bundle 适用场景与选型建议

chaplean/form-handler-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.18k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 07 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 chaplean/form-handler-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 chaplean/form-handler-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 chaplean/form-handler-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-09