m2mtech / api-platform-datatables-format
API Platform 的 Datatables 格式扩展。
Requires
- php: >=7.2.5
- api-platform/core: ^2.5
- symfony/config: ^5.4|^6.0
- symfony/dependency-injection: ^5.4|^6.0
- symfony/http-kernel: ^5.4|^6.0
Requires (Dev)
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^8.5.22|^9.5
- roave/security-advisories: dev-latest
- symfony/phpunit-bridge: ^5.4|^6.0
- symfony/stopwatch: ^5.4|^6.0
- symplify/easy-coding-standard: ^10.0
Suggests
- metaclass-nl/filter-bundle: dev-master
README
此包为 API Platform 提供了 Datatables 格式。
安装
composer require m2mtech/api-platform-datatables-format
如果您未使用 Flex,请启用此包
// config/bundles.php return [ // ... M2MTech\ApiPlatformDatatablesFormat\M2MTechApiPlatformDatatablesFormatBundle::class => ['all' => true], ];
使用方法
启用 datatables 格式
# config/packages/api_platform.yaml api_platform: formats: datatables: [ 'application/vnd.datatables+json' ]
分页
该包将查询参数 start
和 length
从 datatables 重写为 page
和 itemsPerPage
或您为 API Platform 设置的 page_parameter_name
和 items_per_page_parameter_name
。
例如:
- /api/offers?draw=1&start=0&length=10 + /api/offers?draw=1&page=1&itemsPerPage=10
API Platform 中默认启用了分页。
排序
该包将查询参数 columns
和 order
从 datatables 重写为 order
或您为 API Platform 设置的 order_parameter_name
。
例如:
- /api/offers?draw=2&columns[0][data]=name&columns[1][data]=price&order[0][column]=1&order[0][dir]=desc + /api/offers?draw=2&order[email]=desc
您需要为 API Platform 启用排序。
例如,在实体定义中
#[ApiFilter(OrderFilter::class, properties: ['name', 'price'])]
搜索
该包将查询参数 columns
和 search
从 datatables 重写为 or
以供 API Platform 的 Filter 逻辑 使用。
例如:
- /api/offers?draw=3&columns[0][data]=name&columns[1][data]=description&search[value]=shirt + /api/offers?draw=2&or[name]=shirt&or[desciption]=shirt
您需要安装 API Platform 的 Filter 逻辑 或类似的包或您自己的过滤器以实现此功能,例如。
composer require metaclass-nl/filter-bundle "dev-master"
您还需要为 API Platform 启用搜索过滤器。
例如,在实体定义中
#[ApiFilter(SearchFilter::class, properties: ['name' => 'partial', 'description' => 'partial'])] #[ApiFilter(FilterLogic::class)]
输出
输出包括数据以及查询中的 recordsTotal
和 recordsFiltered
(总是相同的)以及 draw
参数。
测试
此包是为 php 7.4 开发的,并已测试与 php 7.2 到 8.1 的兼容性。
composer test
变更日志
有关最近更改的更多信息,请参阅 变更日志。
贡献
有关详细信息,请参阅 贡献指南。
安全漏洞
有关如何报告安全漏洞,请参阅我们的 安全策略。
鸣谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。