aqilixapi/image

用于处理图片的Apigility模块

1.3.1 2015-09-20 02:01 UTC

This package is not auto-updated.

Last update: 2024-09-18 09:30:51 UTC


README

#Images API Handler 此模块旨在处理API中的图片(创建/上传、读取、更新、删除)。它使用MySQL与Doctrine ORM。此模块可扩展以使用其他数据库适配器。

目前此模块支持以下资源,需要OAuth2身份验证

  • POST /v1.0/image
  • GET /v1.0/image/id
  • PATCH /v1.0/image/id
  • DELETE /v1.0/image/id

要获取访问令牌,可以使用此资源 POST /oauth 通过使用一些参数

  • grant_type
  • client_secret
  • client_id
  • username
  • password

依赖项

安装

这是一个ZF2/Apigility模块,因此要在您的ZF2/Apigility项目上使用它,需要在 composer.json 中添加 repositoriesrequire

  "require": {
    .
    .
    .
    "aqilixapi/image": "1.1"
  }

运行 composer update 然后在 config/application.config.php 中启用模块

return array(
    'modules' => array(
       .
       .
       .
       'AqilixAPI\\Image', 
       'ZF\\OAuth2\\Doctrine',
       'DoctrineDataFixtureModule'
    )
)

配置

由于此模块使用 doctrine/doctrine-orm-module,我们只需要使用配置文件配置数据库凭据。我已在此处准备了配置文件 config/doctrine.local.php.dst。只需将此文件复制到 config/autoload/doctrine.local.php 并更改 连接参数

    'connection' => array(
        'orm_default' => array(
            'driverClass' => 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver',
            'params' => array(
                'host'     => '127.0.0.1',
                'dbname'   => 'apigility',
                'user'     => 'apigility',
                'password' => 'apigility',
            ),
        ),
    ),

之后,我们需要配置 图片路径缩略图路径原始文件路径Asset Manager路径。配置文件在这里 config/aqilixapi.image.local.php.dist。将此文件复制到 config/autoload/aqilixapi.image.local.php 并调整这些配置

    'asset_manager' => array(
        'resolver_configs' => array(
            'paths' => array(
                'data/upload',
            ),
        ),
    ),
    'images' => array(
        'asset_manager_resolver_path' => 'data/upload',
        'target' => 'data/upload/images/img',
        'thumb_path' => 'data/upload/images/thumbs',
        'ori_path'   => 'data/upload/images/ori',
    ),

确保这些路径存在并可由 Web服务器 写入,但如果您仅使用 PHP内置Web服务器 进行开发,则无需更改它们的权限。

OAuth2

要启用 OAuth2 身份验证,只需将默认配置文件 (config/oauth2.doctrine-orm.local.php.dist, config/oauth2.local.php.dist) 复制到 config/autoload/oauth2.doctrine-orm.local.phpconfig/autoload/oauth2.local.php

我们还可以根据 Scope 配置 授权。目前,基于 Scope 的ACL仅由 Client Credentials 授权类型 支持。这是由于 ZF-OAUTH2 的限制。要添加另一个 授权类型,我们应该扩展 ZF-OAUTH2 代码。对于配置,只需添加上面提到的配置(《config/autoload/aqilixapi.image.local.php》)

    'authorization' => array(
        'scopes' => array(
            'post' => array(
                'resource' => 'AqilixAPI\Image\V1\Rest\Image\Controller::collection',
                'method' => 'POST',
            ),
            'update' => array(
                'resource' => 'AqilixAPI\Image\V1\Rest\Image\Controller::entity',
                'method' => 'PATCH',
            ),
            'delete' => array(
                'resource' => 'AqilixAPI\Image\V1\Rest\Image\Controller::entity',
                'method' => 'DELETE',
            )
        )
    ),

Scope 名称定义为键,并定义要授权的 资源方法

数据库

此模块使用 imageuser 和其他用于 OAuth2 的表。目前它使用 MySQL,但您可以根据需要轻松更改它,只要数据库由 Doctrine ORM 支持。如果您已遵循上述说明,则意味着只需创建数据库表。

为此,只需从 app骨架 工作目录运行此命令

vendor/bin/doctrine-module orm:schema-tool:create

将创建表,如果您想尝试使用示例数据使用API。我已在源代码中准备好了它们。请运行此命令

vendor/bin/doctrine-module data-fixture:import

然后运行API

php -S 0.0.0.0:8080 -t public public/index.php

示例

以下是在尝试API时制作的截图。我使用 Postman Chrome Extension 作为 REST客户端。您可以使用与截图相同的数据,因为我已在 Data Fixtures 中将其设置为相同。

####使用凭证、客户端ID和客户端密钥从OAuth请求访问令牌 使用凭证、客户端ID和客户端密钥从OAuth请求访问令牌

然后使用访问令牌在发送 请求 到API时在 Authorization头部

#### 使用 POST 方法上传图片使用 POST 方法上传图片

#### 使用 GET 方法检索上传的图片使用 GET 方法检索上传的图片

#### 使用 PATCH 方法更新图片使用 PATCH 方法更新图片

#### 使用 GET 方法检索图片集合使用 GET 方法检索图片集合

Retrieve Images Collection Using GET Method

##### 使用 DELETE 方法删除图片使用 DELETE 方法删除图片