eyja/rest-bundle
此包最新版本(dev-master)没有提供许可证信息。
使用Doctrine实体创建REST API的框架
dev-master
2014-05-09 09:43 UTC
Requires
- php: >=5.3.2
- jms/serializer-bundle: dev-master as 0.11.x-dev
- symfony/symfony: >=2.3
This package is not auto-updated.
Last update: 2024-09-23 13:59:18 UTC
README
进行中
TLDR;从Symfony+Doctrine实体自动生成RESTful API。前往文档查看其外观。
设置
要开始,请在您的Symfony项目中安装此包,并在AppKernel中注册它。
composer require eyja/rest-bundle dev-master
# /app/AppKernel.php
new Eyja\RestBundle\EyjaRestBundle()
最小示例
为您的REST API创建新的包
php app/console generate:bundle \
--no-interaction --namespace=Acme/RestBundle --dir=src --format=yml
更改新包的自动生成路由定义,使其看起来像这样
# app/config/routing.yml
acme_rest:
prefix: /api/v1/
resource: @AcmeRestBundle/Resources/config/routing.yml
type: rest
创建新的Doctrine实体
php app/console generate:doctrine:entity \
--no-interaction --entity=AcmeRestBundle:Cat \
--fields="name:string(100)" --format=yml
php app/console doctrine:schema:update --force
为此实体定义序列化
# src/Acme/RestBundle/Resources/serializer/Entity.Cat.yml Acme\RestBundle\Entity\Cat: properties: id: type: integer groups: [collection, single] name: type: string groups: [collection, single]
创建最简单的控制器,无需类,只需服务
# src/Acme/RestBundle/Resources/config/services.yml services: acme_rest.controller.cat: parent: eyja_rest.abstract_controller arguments: [AcmeRestBundle:Cat, cat] tags: [ { name: rest.controller } ]
太棒了!您可以使用您的新API
php app/console router:debug
rest_cat_getSingle GET ANY ANY /api/v1/cat/{id}
rest_cat_getCollection GET ANY ANY /api/v1/cat
rest_cat_create POST ANY ANY /api/v1/cat
rest_cat_update PUT ANY ANY /api/v1/cat/{id}
rest_cat_delete DELETE ANY ANY /api/v1/cat/{id}
curl -XPOST https:///app_dev.php/cat -HContent-Type:\ application/json \
-d'{"name":"Lucifer"}'
{"id":1,"name":"Lucifer"}
curl -XGET https:///app_dev.php/cat/1
{"id":1,"name":"Lucifer"}
curl -XPUT https:///app_dev.php/cat/1 -HContent-Type:\ application/json \
-d'{"name":"Lucifer -.-"}'
{"id":1,"name":"Lucifer -.-"}
curl -XGET https:///app_dev.php/cat
{"results":[{"id":1,"name":"Lucifer"}],"_metadata":{"limit":20,"offset":0,"total":1}}
curl -XDELETE https:///app_dev.php/cat/1
# Empty response, http status code 204
更大型示例
要查看更复杂的示例,请参阅演示包,其中包含自定义路由和高级序列化。