psx / psx-bundle
PSX Bundle
v0.1.1
2015-12-06 09:19 UTC
Requires
- psx/psx: ~1.1
- symfony/framework-bundle: ~2.2
This package is auto-updated.
Last update: 2024-08-31 00:29:31 UTC
README
此Bundle为Symfony提供基本的PSX功能。PSX是一个用于构建RESTful API的框架。该Bundle包含两个注解Incoming
和Outgoing
,可以指定入站或出站数据的JSONSchema。PSX根据提供的Schema格式化请求和响应。该Bundle注册了kernel.controller
和kernel.response
监听器来处理验证和转换。有关PSX的更多信息,请访问http://phpsx.org。
用法
以下是一个示例控制器,用于展示用法。您必须在控制器方法中使用DataResponse
对象,以便PSX可以转换数据。如果没有定义出站Schema,则所有数据都将直接通过。
<?php namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use PSX\PSXBundle\Configuration\Incoming; use PSX\PSXBundle\Configuration\Outgoing; use PSX\PSXBundle\DataResponse; class DefaultController extends Controller { /** * @Method({"GET"}) * @Route("/", name="index") * @Outgoing("schema/incoming.json") */ public function indexAction(Request $request) { return new DataResponse([ 'firstName' => 'bar', 'lastName' => 'bar' ]); } /** * @Method({"POST"}) * @Route("/", name="new") * @Incoming("schema/outgoing.json") */ public function newAction(Request $request) { $data = $request->attributes->get(Context::REQUEST_BODY); // @TODO work with the parsed data // $data->getFirstName(); return new DataResponse([ 'success' => true, 'message' => 'Success!' ]); } }
使用以下JSON Schema在app/schema/incoming.json
和app/schema/outgoing.json
中
{ "title": "foo", "type": "object", "properties": { "firstName": { "type": "string" }, "lastName": { "type": "string" }, "age": { "description": "Age in years", "type": "integer", "minimum": 0 } }, "required": ["firstName", "lastName"] }