wobblecode/rest-bundle
Composer 安装命令:
composer require wobblecode/rest-bundle
包简介
A bundle that creates REST Apis based on your current controllers actions
README 文档
README
Quick RESTFul Apis For Symofny 3 & 4
A bundle that creates RESTful Apis in seconds, based on your current controllers actions just using annotations. This is very helpful if you need access to a basic RESTful API under the same domain or page.
A practical Case: if you're using Backbone, you can progressive enhance the functionality supporting common CRUD and One simple page CRUD enabling a quick RESTFul API…
These are the main features:
- Works with your current controllers
- Works with your current Symfony forms
- Intercepts current redirects
- Version api support
- Api based on content negotiation
- Integrated with JMSSerializerBundle
- Integrate with KnpPaginatorBundle to obtain metadata
Quick Example
This is the most basic example of use. Just adding the Rest annotation, the api will be available using content negotiation. So if you request JSON, it will return a JSON format schema, If html is requested it will fallback in @Template so the related view will be rendered.
It will use JSMSerializer if it's possible in the model as well.
use WobbleCode\RestBundle\Configuration\Rest;
/**
* @Route("/")
* @Template()
* @Rest()
*/
public function indexAction()
{
...
return array(
'entities' => $entity
);
}
Example using httpie (replace your URL if needed)
http http://localhost:8000 --json
HTTP/1.1 200 OK
Cache-Control: max-age=0, must-revalidate, no-cache, no-store, public, s-maxage=0
Connection: close
Content-Type: application/json
Host: localhost:8000
X-Debug-Token: 243d5e
X-Debug-Token-Link: /_profiler/243d5e
X-Powered-By: PHP/5.5.7
{
"entities": [...],
"metadata": {
"count": 10,
"items_per_page": 10,
"page_number": 1,
"total_count": 244
}
}
Setup
Add to composer
"wobblecode/rest-bundle": "dev-master",
Enable bundle in Kernel
new WobbleCode\RestBundle\WobbleCodeRestBundle()
Options
All examples shows the default values.
output
List of default objects to serialize from the returned array.
/**
* @Rest(output={"entity", "entities", "meta"})
*/
public function indexAction()
{
...
return array(
'user' => $user,
'post' => $post,
'entity' => $entity
);
}
Only entity value will be serialized and returned.
serializeGroups
List of groups to use with the serializer ( see JMSSerializerBundle groups ). By default will serialize all groups if not defined
/**
* @Rest(serializeGroups={"api", "ui-admin"})
*/
acceptedContent
List of accepted headers that enables REST
/**
* @Rest(acceptedContent={"application/json"})
*/
You will have to send Accept: application/json in order to enable the REST api
functionality. If there is no match, it will fallback to the controller view.
defaultAccept
If Accept header is missing you can set a default value with defaultAccept
/**
* @Rest(defaultAccept="application/json")
*/
If the Accept header is missing the default value is
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
payloadMapping
Defines if the payload is assigned to a POST value. This is useful because
forms are usually expecting to be received under a POST variable with the name
of the form Eg: $_POST['my_form_name']
/**
* @Rest(payloadMapping="form")
*/
statusCodeParam
Parameter used to override status code response.
/**
* @Route("/")
* @Template()
* @Rest(statusCodeParam="status_code")
*/
public function indexAction()
{
...
return array(
'status_code' => '403'
);
}
versionRequired
Force to send version in Accept header if true
/**
* @Rest(versionRequired=false)
*/
defaultVersion
Define the default version of the api, false as default for no versioning
interceptRedirects
Intercept 3xx redirects and responds with flash messages
processForms
Process forms errors
defaultFormParam
Process params with name form as principal Form for validation
Configuration
You can set serialize null property on bundle configuration.
wobble_code_rest: serialize_null: false
License
Copyright (c) 2016 Luis Hdez
Released under MIT LICENSE, more info at LICENSE-MIT file.
wobblecode/rest-bundle 适用场景与选型建议
wobblecode/rest-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.65k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2014 年 06 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「rest」 「api」 「restful」 「content negotiation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wobblecode/rest-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wobblecode/rest-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wobblecode/rest-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Bundle Symfony DaplosBundle
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
BlockCypher's PHP SDK for REST API
统计信息
- 总下载量: 12.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-06-04