psx/psx-bundle

PSX Bundle

安装: 28

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

v0.1.1 2015-12-06 09:19 UTC

This package is auto-updated.

Last update: 2024-08-31 00:29:31 UTC


README

此Bundle为Symfony提供基本的PSX功能。PSX是一个用于构建RESTful API的框架。该Bundle包含两个注解IncomingOutgoing,可以指定入站或出站数据的JSONSchema。PSX根据提供的Schema格式化请求和响应。该Bundle注册了kernel.controllerkernel.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.jsonapp/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"]
}