定制 dittto/symfony-custom-request 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

dittto/symfony-custom-request

Composer 安装命令:

composer require dittto/symfony-custom-request

包简介

Allows use of custom requests in controllers

README 文档

README

What is it?

To help accomplish the "Thin controllers, fat models" ideal, this bundle allows you to create custom request objects that you can use for validating your input before it gets to the controller.

You can use these to make sure certain fields are set with certain dynamic values, or validate form data without having to add additional logic to your controller.

The aim is that your controller knows that it has the correct data and therefore can start working with it immediately, instead of having to double-check it's input is valid.

The new custom request object are defined via services, so can have any other services passed to them. This means your validation steps can use data-sources such as other APIs, databases, Elasticsearch, etc.

How to use it

The first step is to update your composer.json with dittto/symfony-custom-request. You may need to also specify the an entry in the repositories for the github repository.

After you've added and installed this bundle, you'll need to add it to the AppKernel.php file:

class AppKernel extends Kernel {
    public function registerBundles() {
        $bundles = [
            ...
            new \Dittto\CustomRequestBundle\DitttoCustomRequestBundle(),

Next we'll create our custom request. This is going to be a simple check that looks for a query string containing token=allowed. The test below is stored at AppBundle/Request/TestRequest.php.

use Dittto\CustomRequestBundle\Request\AbstractRequestType;

class TestRequest extends AbstractRequestType {
    public function validate():bool {
        return $this->getOriginalRequest()->query->get('token') === 'allowed';
    }
}

Update the services for our new TestRequest. The tag is important as this enables us to know this is a custom request and can be used as a controller parameter.

services:
    test_request:
        class: AppBundle\Request\TestRequest
        tags: [ { name: dittto.request } ]

Lastly, we need to tell our controller to use our custom request using it's parameters.

class DefaultController extends Controller {
    public function indexAction(TestRequest $request):Response {
        ...
    }
}

Filters

There is a filter chain available for altering how this code handles itself after the validate() method has been run. By default there is a filter in place that if a GET request fails, then a 400 exception is thrown.

You can add as many additional filters as you like to the chain to create automated responses based on certain request types.

New filters are added by creating a compatible filter (RequestFilterInterface) and adding a tag of dittto.request_filter when defining it as a service.

To make the filters run in a particular order, add a slot as a tag with a positive integer as it's value. These run in the order of lowest number first. If you assign 2 or more filters to the same slot, the first-assigned filter will take the slot and block any others from taking it's place.

How to override default filters

Slots in local configs seem to always install before vendor-defined configs, so you can use the following to replace the default filter.

services:
    override_exception_on_failed_get:
        class: Dittto\CustomRequestBundle\Request\Filter\NullFilterRequest
        tags: [ { name: dittto.request_filter, slot: 10 } ]

dittto/symfony-custom-request 适用场景与选型建议

dittto/symfony-custom-request 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.48k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「controller request」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 dittto/symfony-custom-request 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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