enm/json-api-server-bundle

enm/json-api-server 的 Symfony 集成

3.1.3 2020-01-02 20:01 UTC

README

Build Status

enm/json-api-server 提供的 symfony 集成。

安装

composer require enm/json-api-server-bundle

文档

首先应阅读 enm/json-api-server 的文档,因为此包仅将功能集成到您的 symfony 项目中。

  1. 配置
    1. 配置
    2. 路由
  2. 请求处理器
  3. 错误处理

配置

<?php
// confing/bundles.php
return [
    // ...
    Enm\Bundle\JsonApi\Server\EnmJsonApiServerBundle::class => ['all' => true],
    // ...
];

配置

所有包配置都是可选的。

# config/packages/(dev/|prod/|test/|)enm_json_api.yaml
enm_json_api_server:
    debug: false
    url_prefix: '' # configure this to use a url prefix for your json api routes: e.g. /api/{type}. only needed if a prefix is defined in your routing
    route_name_prefix: 'enm.json_api' # Prefix of the route names in symfony (for exception handling). only needed if a nam prefix is defined in your routing

路由

# app/config/routing.yml | config/routes.yaml
json_api:
  resource: "@EnmJsonApiServerBundle/Resources/config/routing.xml"

如果您使用预定义的路由(没有 api 前缀配置),则以下路由将被匹配

GET /{type}

GET /{type}/{id}

GET /{type}/{id}/relationships/{relationship}

GET /{type}/{id}/{relationship}

POST /{type}

PATCH /{type}/{id}

DELETE /{type}/{id}

POST /{type}/{id}/relationships/{relationship}

PATCH /{type}/{id}/relationships/{relationship}

DELETE /{type}/{id}/relationships/{relationship}

请求处理器

每个请求处理器可以通过服务容器简单地注册(标签:json_api_server.request_handler

AppBundle\RequestHandler\YourRequestHandler:
    tags:
      - { name: json_api_server.request_handler, type: 'myResources' }

标签属性 type 必须包含将由此请求处理器处理的 json api 资源类型。

错误处理

该包将处理所有异常并将它们转换为有效的 json api 错误响应。