notafacil/image

API用于消费apigility项目资源,如静态图片和文件管理

安装: 2

依赖: 0

建议者: 0

安全: 0

星星: 0

观察者: 2

分支: 0

开放问题: 0

类型:项目

0.1.1 2018-02-19 19:57 UTC

This package is not auto-updated.

Last update: 2024-09-20 01:29:03 UTC


README

#Images API处理器 该模块旨在处理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": {
    .
    .
    .
    "notafacil/image": "1.1"
  }

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

return array(
    'modules' => array(
       .
       .
       .
       'Notafacil\\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/notafacil.image.local.php.dist。将此文件复制到config/autoload/notafacil.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

我们还可以根据范围配置授权。目前,仅通过Client Credentials 授权类型支持基于Scope的访问控制列表(ACL)。这是由于ZF-OAUTH2的限制。要添加另一个授权类型,我们应该扩展ZF-OAUTH2代码。对于配置,只需添加上述配置(config/autoload/notafacil.image.local.php)。

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

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

数据库

此模块使用一张image表、一张user表和几张用于OAuth2的其他表。目前它使用MySQL,但您可以根据需要轻松更改它,只要数据库由Doctrine ORM支持即可。如果您已经按照上述说明操作,这意味着只需创建数据库表。

为此,只需从app skeleton工作目录运行以下命令

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 Header中使用访问令牌

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

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

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

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

Retrieve Images Collection Using GET Method

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