paneedesign/api-bundle

Symfony3项目的API管理。

安装: 3,080

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:symfony-bundle

v4.0.5 2019-09-25 08:24 UTC

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_atapi_refresh_token_expire_at
  • api_client_idapi_client_secret 添加到参数中