一个类似于 REST 生成器捆绑的 CRUD 组件,适用于 Symfony 2
A CRUD like REST Generator
Require the "voryx/restgeneratorbundle" package in your composer.json and update your dependencies.
$ php composer.phar require voryx/restgeneratorbundle dev-master
Add the VoryxRestGeneratorBundle to your application's kernel along with other dependencies:
public function registerBundles()
{
$bundles = array(
//...
new Voryx\RESTGeneratorBundle\VoryxRESTGeneratorBundle(),
new FOS\RestBundle\FOSRestBundle(),
new JMS\SerializerBundle\JMSSerializerBundle($this),
new Nelmio\CorsBundle\NelmioCorsBundle(),
//...
);
//...
}
This bundle depends on a number of other Symfony bundles, so they need to be configured in order for the generator to work properly
…
Generate the REST controller
$ php app/console voryx:generate:rest
This will guide you through the generator which will generate a RESTful controller for an entity.
Create a new entity called 'Post':
$ php app/console doctrine:generate:entity --entity=AppBundle:Post --format=annotation --fields="name:string(255) description:string(255)" --no-interaction
Update the database schema:
$ php app/console doctrine:schema:update --force
Generate the API controller:
$ php app/console voryx:generate:rest --entity="AppBundle:Post"
If you selected the default options you'll be able to start using the API like this:
Creating a new post (POST)
$ curl -i -H "Content-Type: application/json" -X POST -d '{"name" : "Test Post", "description" : "This is a test post"}' http://localhost/app_dev.php/api/posts
Updating (PUT)
$ curl -i -H "Content-Type: application/json" -X PUT -d '{"name" : "Test Post 1", "description" : "This is an updated test post"}' http://localhost/app_dev.php/api/posts/1
Get all posts (GET)
$ curl http://localhost/app_dev.php/api/posts
Get one post (GET)
$ curl http://localhost/app_dev.php/api/posts/1
Delete (DELETE)
$ curl -X DELETE http://localhost/app_dev.php/api/posts/1
If you want the form to be able to convert related entities into the correct entity id on POST, PUT or PATCH, use the voryx_entity form type
#Form/PostType()
->add(
'user', 'voryx_entity', array(
'class' => 'Acme\Bundle\Entity\User'
)
)
暂无开放 Issues,或尚未同步最近议题。