bauer01/unimapper-nette

UniMapper 的官方 Nette 扩展

v1.2.1 2015-12-02 14:02 UTC

This package is auto-updated.

Last update: 2024-09-14 10:26:42 UTC


README

Build Status

官方 Unimapper 扩展,用于 Nette 框架。

安装

$ composer require unimapper/nette:@dev

Nette 2.1 及以上版本

config.neon 中注册扩展。

extensions:
    unimapper: UniMapper\Nette\Extension

Nette 2.0

app/bootstrap.php 中注册扩展。

UniMapper\Nette\Extension::register($configurator);

return $configurator->createContainer();

配置

unimapper:
    adapters:
        Mongo: @service
        MySQL: @anotherService
        ...
    cache: true
    namingConvention:
        entity: 'YourApp\Model\*'
        repository: 'YourApp\Repository\*Repository'
    api:
        enabled: false
        module: "Api"
        route: true
    panel:
        enabled: true
        ajax: true # log queries in AJAX requests
    profiler: true
    customQueries:
        - CustomQueryClass
        - ...

API

为您的应用程序创建新的 API 非常简单,您只需要为每个实体创建一个表示器。

请记住,每个 API 表示器都应该始终扩展 UniMapper\Nette\Api\Presenter

namespace YourApp\ApiModule\Presenter;

class EntityPresenter extends \UniMapper\Nette\Api\Presenter
{
    ...
}

现在您可以调用标准 API 方法,如

GET

  • 关联 - 通用参数,用于告诉响应中应包含哪些关联。语法应如下所示:?associate[]=property1&associate[]=property2?associate=property1,property2

** 响应**

{
    "body": {..}
}

/api/entity/id

获取单个记录。

/api/entity

获取所有记录。

  • count - 可选参数,如果 ?count=true 设置,则响应体中将返回项目数量而不是数据。
  • limit - 最大限制设置为 10。您可以通过覆盖您的 API 表示器派生类中的 $maxLimit 属性来更改它。
  • offset
  • where

PUT

/api/entity

使用存储在请求体中的 JSON 数据更新所有记录。可以设置 过滤,并且响应体包含受影响记录的数量。

** 响应**

{
    "body": 3,
    "success": true
}

/api/entity/id

使用存储在请求体中的 JSON 数据更新单个记录。

** 响应**

{
    "success": true
}

POST

使用存储在请求体中的 JSON 数据创建新的记录,并在响应体中返回新实体的主键值。

/api/entity

** 响应**

{
    "success": true,
    "link": "url to created entity",
    "body": "id of created entity"
}

DELETE

/api/entity

删除响应体中包含的记录数量。

** 响应**

{
    "body": {..}
    "success": true
}

/api/entity/id

删除单个记录。

** 响应**

{
    "success": true
}

自定义 API 方法

您甚至可以定义自己的自定义方法。

namespace YourApp\ApiModule\Presenter;

class EntityPresenter extends \UniMapper\Nette\Api\Presenter
{
    public function actionYourCustomMethod($id)
    {
        ...
    }
}

然后您可以像这样进行请求:/api/entity/1?action=yourCustomMehod

数据过滤

过滤器可以设置为 URL 中的 GET 参数 where。它应在此处为如 此处 所描述的有效 JSON 格式。

错误响应

如果检测到某些不良请求或发生错误,则返回的响应可能如下所示

{
    "success": false
    "code": 405,
    "messages": []
}

生成链接

在您的模板中,只需使用标准的 Nette 链接宏。

  • {link :Api:Entity:get}
  • {link :Api:Entity:get 1}
  • {link :Api:Entity:put 1}
  • {link :Api:Entity:post}
  • {link :Api:Entity:action}

用法

您甚至可以使用此 API 构建其他应用程序,只需在您的 config.neon 中注册官方 API 适配器类 UniMapper\Nette\Api\Adapter

自定义请求工厂

为了更容易地进行 API 查询,您可以在您的 config.neon 中将工厂接口注册为动态服务。

services:
    - UniMapper\Nette\Api\ICustomRequestFactory

在您的仓库中的使用情况可能如下所示

class SomeRepository extends \UniMapper\Repository
{
    private $requestFactory;

    public function __construct(
        \UniMapper\Connection $connection,
        \UniMapper\Nette\Api\ICustomRequestFactory $requestFactory
    ) {
        parent::__construct($connection);
        $this->requestFactory;
    }

    public function getSomethingFromApi()
    {
        $this->requestFactory()->setResource("apiResource")->setAction("custom")->send();
    }
}