m6web/fos-rest-extra-bundle

关于此包的最新版本(v4.0.0)没有提供许可证信息。

FOSRestBundle的额外功能

安装数: 156,042

依赖项: 0

建议者: 0

安全性: 0

星标: 1

关注者: 48

分支: 3

开放性问题: 5

类型:symfony-bundle

v4.0.0 2022-07-21 12:19 UTC

This package is auto-updated.

Last update: 2024-09-21 17:04:06 UTC


README

FOSRestBundle提供额外功能。

依赖

FOSRestExtraBundle需要FOSRestBundle

安装

在您的composer.json文件中添加此行

{
    "require": {
        "m6web/fos-rest-extra-bundle": "~1.0"
    }
}

更新您的供应商

$ composer update m6web/fos-rest-extra-bundle

将以下内容添加到您的AppKernel.php

new FOS\RestBundle\FOSRestBundle(),
new M6Web\Bundle\FOSRestExtraBundle\M6WebFOSRestExtraBundle(),

然后修改您应用程序的FOSRestBundle配置以添加

fost_rest:
    param_fetcher_listener: true

配置

m6_web_fos_rest_extra:
    param_fetcher:

        # Define if extra parameters are allowed. The behavior defined here is the default one and can
        # be overrided by a "RestrictExtraParam" annotation on the current action.
        # Optionnal, true by default
        allow_extra: true

        # Define if all parameters are strict. If true, all given parameters have to match defined
        # format for each on of them.
        # Optionnal, false by default
        strict: false

        # HTTP status code of throwed exception on query with invalid parameters
        # Optionnal, 400 by default
        error_status_code: 400

使用方法

  • RestrictExtraParam(true/false) 注解:允许(false)或禁止(true)未知参数,默认为true
use FOS\RestBundle\Controller\Annotations\QueryParam;
use M6Web\Bundle\FOSRestExtraBundle\Annotation\RestrictExtraParam;

/**
 * TestController
 */
class TestController
{
    /**
     * Restricted controller : only "param1" is permitted
     *
     * @return void
     *
     * @RestrictExtraParam(true)
     *
     * @QueryParam(name="param1", requirements="\d+", nullable=true, description="My Param 1")
     */
    public function getRestrictedAction() {

    }

    /**
     * Unrestricted controller : "param1" and unknown parameters are permitted
     * except if bundle configuration doesn't allow it
     *
     * @QueryParam(name="param1", requirements="\d+", nullable=true, description="My Param 1")
     *
     * @return void
     */
    public function getNonRestrictedAction() {

    }
}

运行测试

$ bin/atoum