aqilixapi / image
用于处理图片的Apigility模块
Requires
- php: >=5.3.3
- doctrine/doctrine-orm-module: =0.8.0
- hounddog/doctrine-data-fixture-module: 0.0.4
- zfcampus/zf-oauth2-doctrine: 0.3.5
Requires (Dev)
- phpmd/phpmd: 2.*
- phpunit/phpunit: 4.*
- squizlabs/php_codesniffer: 1.4.*
- zend/zend-studio-development-mode: ~1.0
- zendframework/zendframework: >=2.3.2,<3.0.0
- zfcampus/zf-apigility: 1.0.3
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 中添加 repositories 和 require。
"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.php 和 config/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 名称定义为键,并定义要授权的 资源 和 方法。
数据库
此模块使用 image、user 和其他用于 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请求访问令牌 
然后使用访问令牌在发送 请求 到API时在 Authorization头部 上





