rnd-cosoft/api-tools

Laminas API Tools 模块,用于 Laminas

1.8.1 2024-05-10 08:19 UTC

README

Build Status

🇷🇺 俄罗斯公民

我们是来自不同国家的 Laminas 成员。我们中很多人在俄罗斯和乌克兰都有朋友、亲戚和同事。我们中的一些人出生在俄罗斯。我们中的一些人现在住在俄罗斯。我们中的一些人的祖父母在二战中与法西斯作战。在这里,没有人支持法西斯主义。

我们中的一位成员有一位乌克兰亲戚,她带着儿子逃离了家园。火车因为前方的轰炸而延误。我们有一些朋友在防空洞里躲避。我们在空袭后焦急地等待他们的消息,空袭不分青红皂白地袭击医院、学校、幼儿园和住宅。我们不是从任何媒体上得知这些情况。这是我们亲身体验的。

您信任我们足够,以至于使用我们的程序,我们请求您再次信任我们。我们需要帮助。走出家门,抗议这场毫无意义的战争。停止杀戮。说“停止战争!”

🇺🇸 致俄罗斯公民

Laminas 团队的成员来自世界各地。我们中很多人在俄罗斯和乌克兰都有朋友、家人和同事。我们中的一些人出生在俄罗斯。我们中的一些人现在住在俄罗斯。我们中的一些人的祖父母在二战中与纳粹作战。在这里,没有人支持法西斯主义。

团队中的一位成员有一位乌克兰亲戚,她和儿子一起逃离了家园。火车因为前方的轰炸而延误。我们有一些朋友在防空洞里躲避。我们在空袭后焦急地等待他们的消息,空袭不分青红皂白地袭击医院、学校、幼儿园和住宅。我们不是从任何媒体上得知这些情况。这是我们亲身体验的。

您信任我们足够,以至于使用我们的软件。我们请求您信任我们,说出真相。我们需要您的帮助。走出家门,抗议这场无谓的战争。停止杀戮。说“停止战争!”

简介

Laminas 的元模块,结合了以下功能:

  • api-tools-api-problem
  • api-tools-content-negotiation
  • api-tools-content-validation
  • api-tools-hal
  • api-tools-mvc-auth
  • api-tools-rest
  • api-tools-rpc
  • api-tools-versioning

以提供一个连贯的解决方案,用于公开基于 Web 的 API。

还提供与数据库连接的 REST 资源。

需求

请参阅 composer.json 文件。

安装

运行以下 composer 命令

$ composer require laminas-api-tools/api-tools

或者,手动将以下内容添加到您的 composer.json 文件的 require 部分

"require": {
    "laminas-api-tools/api-tools": "^1.3"
}

然后运行 composer update 以确保模块已安装。

最后,将模块名称添加到项目配置文件 config/application.config.php 中的 modules 键下

return [
    /* ... */
    'modules' => [
        /* ... */
        'Laminas\ApiTools',
    ],
    /* ... */
];

laminas-component-installer

如果您使用 laminas-component-installer,该插件将为您将 api-tools 以及所有依赖的模块作为应用程序配置中的模块安装。

资产

如果您正在使用此模块与 admin 和/或 欢迎屏幕 一起使用,此模块包含您需要使网站可访问的资产。为此,您有两个选项

  • rwoverdijk/assetmanager 是一个 Laminas 模块,它提供了围绕网络资产管理的高级功能,并且是该模块使用的原始工具。然而,在当前版本(1.6.0)中,它不支持 Laminas 的 v3 组件。即将推出的 1.7.0 版本可能将支持它们。
  • laminas-api-tools/api-tools-asset-manager 是一个 Composer 插件,它在安装和卸载包期间运行,根据 rwoverdijk/assetmanager 中的配置复制和删除资产树。但是,要使用此插件,您需要先安装插件,然后再安装此模块。(如果您已经安装了此模块,请使用 composer remove laminas-api-tools/api-tools 命令删除它。)

配置

用户配置

此模块用户配置的最高级配置键是 api-tools

db-connected

db-connected 是一个资源数组,可以在需要满足基于数据库表驱动的资源用例时,通过 TableGatewayAbstractFactoryDbConnectedResourceAbstractFactory 构建。以下示例列举了启用此功能所需的所有必要和可选配置。

示例

'db-connected' => [
    /**
     * This is sample configuration for a DB-connected service.
     * Each such service requires an adapter, a hydrator, an entity, and a
     * collection.
     *
     * The TableGateway will be called "YourDBConnectedResource\Table" should
     * you wish to retrieve it manually later.
     */
    'YourDBConnectedResource' => [
        'table_service'    => 'Optional; if present, this service will be used as the table gateway',
        'resource_class'   => 'Optional; if present, this class will be used as the db-connected resource',
        'table_name'       => 'Name of DB table to use',
        'identifier_name'  => 'Optional; identifier field in table; defaults to table_name_id or id',
        'adapter_name'     => 'Service Name for DB adapter to use',
        'hydrator_name'    => 'Service Name for Hydrator to use',
        'entity_class'     => 'Name of entity class to which to hydrate',
        'collection_class' => 'Name of collection class which iterates entities; should be a Paginator extension',
    ],
],

系统配置

以下配置对于确保此模块在 Laminas 框架应用程序中正常工作至关重要,并由模块提供

namespace Laminas\ApiTools;

use Laminas\Db\Adapter\AdapterAbstractServiceFactory as DbAdapterAbstractServiceFactory;
use Laminas\ServiceManager\Factory\InvokableFactory;

return [
    'asset_manager' => [
        'resolver_configs' => [
            'paths' => [
                __DIR__ . '/../asset',
            ],
        ],
    ],
    'router' => [
        'routes' => [
            'api-tools' => [
                'type'  => 'literal',
                'options' => [
                    'route' => '/api-tools',
                ],
                'may_terminate' => false,
            ],
        ],
    ],
    'service_manager' => [
        'factories' => [
            MvcAuth\UnauthenticatedListener::class => InvokableFactory::class,
            MvcAuth\UnauthorizedListener::class => InvokableFactory::class,
        ],
        'abstract_factories' => [
            DbAdapterAbstractServiceFactory::class, // so that db-connected works "out-of-the-box"
            DbConnectedResourceAbstractFactory::class,
            TableGatewayAbstractFactory::class,
        ],
    ],
];

Laminas 事件

监听器

Laminas\ApiTools\MvcAuth\UnauthenticatedListener

此监听器连接到 MvcAuthEvent::EVENT_AUTHENTICATION_POST 事件的优先级为 100。此监听器的主要目的是覆盖 api-tools-mvc-auth未认证 监听器,以便在认证失败时能够响应 API-Problem 响应(而不是标准 HTTP 响应)。

Laminas\ApiTools\MvcAuth\UnauthorizedListener

此监听器连接到 MvcAuthEvent::EVENT_AUTHORIZATION_POST 事件的优先级为 100。此监听器的主要目的是覆盖 api-tools-mvc-auth未授权 监听器,以便在授权失败时能够响应 API-Problem 响应(而不是标准 HTTP 响应)。

Laminas\ApiTools\Module

此监听器连接到 MvcEvent::EVENT_RENDER 事件的优先级为 400。其目的是在 MvcEvent 的结果为 HalJsonModelJsonModel 时,有条件地附加 Laminas\ApiTools\ApiProblem\RenderErrorListener,以确保在渲染错误发生的情况下,api-tools-api-problem 可以渲染响应。

Laminas 服务

工厂

Laminas\ApiTools\DbConnectedResourceAbstractFactory

此工厂除了使用请求的名称外,还使用 api-tools.db-connected 配置来生成基于 Laminas\ApiTools\DbConnectedResource 的资源。

Laminas\ApiTools\TableGatewayAbstractFactory

此工厂除了使用请求的名称外,还使用 api-tools.db-connected 配置来生成正确配置的 Laminas\Db\TableGateway\TableGateway 实例。这些 TableGateway 实例配置为使用正确的 HydratingResultSet,并在迭代时为每一行返回配置的实体。

模型

Laminas\ApiTools\DbConnectedResource

此实例作为数据库连接的 REST 资源类的基类。此实现是 Laminas\ApiTools\Rest\AbstractResourceListener 的扩展,并且可以作为 RESTful 资源被 Laminas API Tools 路由。