wizards/rest-bundle
Composer 安装命令:
composer require wizards/rest-bundle
包简介
Integration of php-rest-api into Symfony.
README 文档
README
Symfony bundle for wizards/php-rest-api
Helps you create a REST API in an expressive and streamlined way. It will help you conceive mature and discoverable APIs, thanks to the jsonapi specification. Regardless of the output serialization, the request format will be the same. Have a look at http://github.com/wizardstechnologies/php-rest-api for further documentation and explanations. You can find an example project on https://github.com/BigZ/promoteapi
Requirements
symfony >= 4.4
php >= 7.3
Installation
composer require wizards/rest-bundle
Configuration
Create a configuration file with the following values:
# config/bundles/wizards_rest.yaml
wizards_rest:
data_source: orm|array # Choose between ORM and Array for your data source. More will be added soon
reader: annotation|array
format: jsonapi|array
base_url: your_url
Usage
Create a REST API the easy and configurable way !
This bundle ease the use of wizard's php rest api, and provide some extra goodies for symfony:
- a subscriber to automatically serialize your responses
- a subscriber to automatically serialize your exceptions
- a param converter to inject PSR-7 requests in your controllers
- a multi-part exception to easily serialize multiple errors (such as the one from forms)
- a controller trait that helps on serializing input data from json and jsonapi
If you use symfony flex, those services will be automatically registered.
To serialize a single resource, just return the object from a controller:
public function getArtistAction(string $id, EntityManagerInterface $entityManager)
{
try {
$artist = $entityManager->find(Artist::class, $id);
} catch (\Exception $exception) {
throw new NotFoundHttpException('Artist not found.');
}
return $artist;
}
Note that we don't use the param injector for the entity as we want to be able to dispatch an error ourselves so it is properly formatted.
To Serialize a collection, use the collectionManager
public function getArtistsAction(CollectionManager $collectionManager, ServerRequestInterface $request)
{
return $collectionManager->getPaginatedCollection(Artist::class, $request);
}
To deserialize input, use the json trait
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Wizards\RestBundle\Controller\JsonControllerTrait;
class ArtistController extends AbstractController
{
use JsonControllerTrait;
public function postArtistAction(Request $request, EntityManagerInterface $entityManager)
{
$artist = new Artist();
$form = $this->createForm(ArtistType::class, $artist);
$this->handleJsonForm($form, $request);
if (!$form->isValid()) {
$this->throwRestErrorFromForm($form);
}
$entityManager->persist($artist);
$entityManager->flush();
return $artist;
}
}
wizards/rest-bundle 适用场景与选型建议
wizards/rest-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.92k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「api」 「HATEOAS」 「jsonapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wizards/rest-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wizards/rest-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wizards/rest-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP implementation of HAL http://stateless.co/hal_specification.html
Add the resource system to symfony application
Simple resource and serialization system for application
A PSR-7 compatible library for making CRUD API endpoints
Integration of Hateoas into Symfony2.
Api bundle
统计信息
- 总下载量: 4.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-20