paneedesign / api-bundle
Symfony3项目的API管理。
v4.0.5
2019-09-25 08:24 UTC
Requires
- php: ^5.6|^7.0
- adoy/oauth2: ^1.3
- friendsofsymfony/oauth-server-bundle: ^1.6
- friendsofsymfony/rest-bundle: ^2.3
- jms/serializer-bundle: ^2.0
- nelmio/api-doc-bundle: ^3.1
- symfony/framework-bundle: ^3.2
Requires (Dev)
- incenteev/composer-parameter-handler: ~2.0
- phpspec/phpspec: ^3.2
- phpunit/phpunit: ^5.6
- symfony/security-bundle: ^3.2
README
Symfony3项目的API管理。
安装
步骤 1: 下载Bundle
由于Pane&Design仓库是私有的,所以请将以下 vcs
添加到 composer.json
"repositories": [ ... { "type" : "vcs", "url" : "git@bitbucket.org:paneedesign/api-bundle.git" } ], ... "require": { ... "paneedesign/api-bundle": "^3.0" }
打开命令行,进入您的项目目录,并执行以下命令以下载此bundle的最新稳定版本
$ composer require "paneedesign/api-bundle"
此命令需要您全局安装Composer,具体请参阅Composer文档中的安装章节。
步骤 2: 启用Bundle
然后,通过将其添加到项目中的 app/AppKernel.php
文件中注册的bundle列表中启用该bundle
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new \FOS\OAuthServerBundle\FOSOAuthServerBundle(), new \FOS\RestBundle\FOSRestBundle(), new \JMS\SerializerBundle\JMSSerializerBundle(), new \PaneeDesign\ApiBundle\PedApiBundle(), ); // ... if (in_array($this->getEnvironment(), ['dev', 'test'], true)) { // ... $bundles[] = new \Nelmio\ApiDocBundle\NelmioApiDocBundle(); } } // ... }
步骤 3: 配置
添加参数
// app/config/parameters.yml.dist
parameters:
...
api_server_host: 'https://api.paneedesign.com'
api_type: oauth2
api_client_id: ~
api_client_secret: ~
api_user_class: PaneeDesign\UserBundle\Entity\User
添加配置
// app/config/config.yml imports: - { resource: "@PedApiBundle/Resources/config/config.yml" } ...
// app/config/config_dev.yml imports: - { resource: "@PedApiBundle/Resources/config/custom/nelmio_api_doc.yml" } ...
// app/config/routing.yml ped_api: resource: '@PedApiBundle/Resources/config/routing.yml' prefix: / ...
实现API
// app/config/custom/api.yml public_v1: type: rest prefix: /v1 resource: "@PedApiBundle/Controller/Api/ApiPublicController.php" name_prefix: api_1_ ...
生成客户端
php bin/console fos:oauth-server:create-client --redirect-uri="https://api.paneedesign.com/authorize" --grant-type="https://api.paneedesign.com/oauth/v2/api_key" --grant-type="password" --grant-type="refresh_token"
从2.x升级到3.x
- 将 getApiKeyAccessToken 重命名为 getOAuthToken
- 将
throwRefreshTokenJsonException
的签名从protected function throwRefreshTokenJsonException(JsonException $jsonException, $expiredAt)
更改为protected function throwRefreshTokenJsonException(\OAuth2\OAuth2ServerException $oAuthException)
- 将
refreshTokenResponse
的签名从protected function refreshTokenResponse(User $user, $accessToken)
更改为protected function refreshTokenResponse(User $user, array $oAuthToken)
- 从参数中删除
api_access_token_expire_at
和api_refresh_token_expire_at
- 将
api_client_id
和api_client_secret
添加到参数中