innmind/rest-server-bundle

此包已被弃用且不再维护。未建议替代包。

innmind/rest-server 的 Symfony 集成

3.0.0 2017-09-30 12:02 UTC

This package is auto-updated.

Last update: 2022-02-01 12:59:53 UTC


README

master develop
Scrutinizer Code Quality Scrutinizer Code Quality
Code Coverage Code Coverage
Build Status Build Status

SensioLabsInsight

安装

通过 composer

composer require innmind/rest-server-bundle

在您的项目的 app/AppKernel.php 中添加以下行以启用该包

// app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Innmind\Rest\InnmindRestServerBundle,
        );
        // ...
    }
    // ...
}

然后,您需要指定您允许的应用类型,以下是一个示例

innmind_rest_server:
    accept:
        json:
            priority: 10
            media_types:
                application/json: 0
        html:
            priority: 0
            media_types:
                text/html: 10
                application/xhtml+xml: 0
    content_type:
        json:
            priority: 0
            media_types:
                application/json: 0

在这里,您定义您可以通过 jsonhtml 暴露您的资源。如果客户端接受任何类型的内容,它将自动以 json 的形式暴露数据,因为它具有最高的优先级。客户端可以使用 text/htmlapplication/xhtml+xml 作为其 Accept 标头的媒体类型,以便我们以 html 的形式暴露数据。

我们还描述了发送到我们的 API 的资源必须是 json 的,以及客户端发送的 Content-Type 标头必须是 application/json,否则他将收到错误。

为了正常工作,这里任何媒体类型都必须有一个对应的 序列化编码器supportsEncoding 必须检查 request_{media_type} 格式,示例)。

然后您需要通过添加以下配置来激活路由器

# app/config/routing.yml
rest:
    type: innmind_rest
    resource: .

配置的最后部分是在您的包的 Resources/config 文件夹下创建一个名为 rest.yml 的文件,其中将包含您资源的定义。以下是一个扩展的示例

blog:
    resources:
        blog:
            identity: uuid
            gateway: command
            properties:
                uuid:
                    type: string
                title:
                    type: string
                    access: [READ, CREATE, UPDATE]
                content:
                    type: string
                    access: [READ, CREATE, UPDATE]
                tags:
                    type: set
                    options:
                        inner: string
                author:
                    type: string # identifier of the author
    children:
        meta:
            resources:
                author:
                    identity: uuid
                    gateway: command
                    properties:
                        uuid:
                            type: string
                        name:
                            type: string

现在所有配置都已完成,您需要创建一个实现 GatewayInterface 接口的服务的服务,并使用 innmind_rest_server.gateway 标签定义服务,并附上您选择的别名;别名是您在配置资源时使用的(如上面所示,在这种情况下是 command)。

此类服务定义应如下所示

services:
    my_gateway:
        class: AppBundle\Gateway\MyGateway
        tags:
            - { name: innmind_rest_server.gateway, alias: command }