承接 tobion/openapi-symfony-routing 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tobion/openapi-symfony-routing

Composer 安装命令:

composer require tobion/openapi-symfony-routing

包简介

Loads routes in Symfony based on OpenAPI/Swagger annotations

README 文档

README

Loads routes in Symfony based on OpenAPI/Swagger annotations.

CI

Installation

$ composer require tobion/openapi-symfony-routing

Version >= 1.2 requires zircote/swagger-php 3.x which is compatible with the OpenAPI Specification version 3. Version < 1.2 requires zircote/swagger-php 2.x which works with the OpenAPI Specification version 2 (fka Swagger). So tobion/openapi-symfony-routing can be used with both OpenAPI v2 and v3 and composer will select the compatible one for your dependencies. Route loading stays the same between those versions. You just need to update the annotations when migrating from OpenAPI v2 to v3.

Basic Usage

This library allows to (re-)use your OpenAPI documentation to configure the routing of your Symfony-based API. All the relevant routing information like the HTTP method, path and parameters are already part of the OpenAPI spec. This way you do not have to duplicate any routing information in Symfony. Consider having the controllers annotated with zircote/swagger-php like the following example:

use OpenApi\Annotations as OA;

/**
 * @OA\OpenApi(
 *     @OA\Info(title="My API", version="1.0")
 * )
 */
class MyController
{
    /**
     * @OA\Get(
     *     path="/foobar",
     *     @OA\Response(response="200", description="Success")
     * )
     */
    public function __invoke()
    {
    }
}

This library provides an OpenApiRouteLoader that you need to define as service and configure where to look for annotations like so:

# config/services.yaml
services:
    Tobion\OpenApiSymfonyRouting\OpenApiRouteLoader:
        autoconfigure: true
        # Looks for OpenAPI/Swagger annotations in the symfony flex default "src" directory
        factory: [Tobion\OpenApiSymfonyRouting\OpenApiRouteLoader, fromSrcDirectory]

Then you need to tell Symfony to load routes using it:

# config/routes.yaml
openapi_routes:
    resource: Tobion\OpenApiSymfonyRouting\OpenApiRouteLoader
    type: service

Advanced Features

Scanning annotations in different directories

services:
    Tobion\OpenApiSymfonyRouting\OpenApiRouteLoader:
        autoconfigure: true
        factory: [Tobion\OpenApiSymfonyRouting\OpenApiRouteLoader, fromDirectories]
        arguments:
            - '%kernel.project_dir%/src'
            - '%kernel.project_dir%/vendor/acme/my-bundle/src'

Naming routes

By default routes are auto-named based on the controller class and method. If you want to give routes an explicit name, you can do so using the OpenAPI operationId property:

use OpenApi\Annotations as OA;

class MyController
{
    /**
     * @OA\Get(
     *     path="/foobar",
     *     operationId="my-name",
     *     @OA\Response(response="200", description="Success")
     * )
     */
    public function __invoke()
    {
    }
}

Add format suffix automatically

If your API supports different formats it is often common to optionally allow specifying the requested format as a suffix to the endpoint instead of having to always change headers for content negotiation. The routing loader allows to add a .{_format} placeholder automatically to the routes. This is disabled by default and can be enabled using a format-suffix OpenAPI vendor extension:

use OpenApi\Annotations as OA;

class MyController
{
    /**
     * @OA\Get(
     *     path="/foobar",
     *     x={"format-suffix": {
     *         "enabled": true,
     *         "pattern": "json|xml"
     *     }},
     *     @OA\Response(response="200", description="Success")
     * )
     */
    public function __invoke()
    {
    }
}

The above example will create a route /foobar.{_format} where the format is optional and can be json or xml. You can also enable the format-suffix globally by configuring it on the root OpenApi annotation and disable it for certain routes again, see test fixtures.

Order routes with priority

Since Symfony 5.1, the order of routes defined using annotations can be influenced using a priority. This can be used to make sure templated routes do not match before concrete routes without parameters for the same URL. The priority can also be set on OpenAPI annotations using a priority vendor extension:

use OpenApi\Annotations as OA;

class MyController
{
    /**
     * @OA\Get(
     *     path="/foobar",
     *     x={"priority": 10},
     *     @OA\Response(response="200", description="Success")
     * )
     */
    public function __invoke()
    {
    }
}

Contributing

To run tests:

$ composer install
$ vendor/bin/simple-phpunit

tobion/openapi-symfony-routing 适用场景与选型建议

tobion/openapi-symfony-routing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.97k 次下载、GitHub Stars 达 41, 最近一次更新时间为 2020 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「annotations」 「routing」 「router」 「routes」 「OAI」 「swagger」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tobion/openapi-symfony-routing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tobion/openapi-symfony-routing 我们能提供哪些服务?
定制开发 / 二次开发

基于 tobion/openapi-symfony-routing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 42
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 41
  • Watchers: 3
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-30