halloverden/symfony-route-deprecation-bundle

路由弃用包为您的Symfony应用程序提供了弃用路由的工具。它实现了IETF关于《弃用HTTP头部字段》的草案。

3.1.0 2024-07-03 15:22 UTC

This package is auto-updated.

Last update: 2024-09-03 15:48:45 UTC


README

路由弃用包为您的Symfony应用程序提供了弃用路由的工具。它实现了《弃用HTTP头部字段》的IETF草案

安装

确保全局已安装Composer,如Composer文档中的安装章节所述。

使用Symfony Flex的应用程序

打开命令行控制台,进入您的项目目录,然后执行

$ composer require halloverden/symfony-route-deprecation-bundle

未使用Symfony Flex的应用程序

步骤 1: 下载包

打开命令行控制台,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本

$ composer require halloverden/symfony-route-deprecation-bundle

步骤 2: 启用包

然后,通过将其添加到项目config/bundles.php文件中注册的包列表来启用包

// config/bundles.php

return [
    // ...
    HalloVerden\RouteDeprecationBundle\HalloVerdenRouteDeprecationBundle::class => ['all' => true],
];

用法

您可以使用DeprecatedRoute注解弃用任何路由。

  • since(必需)- 是一个字符串 ("yyyy-mm-dd"),定义了路由何时开始弃用。响应中将设置Deprecation头部,如下所示:Deprecation: @1672531200
  • sunset(可选)- 是一个字符串 ("yyyy-mm-dd"),定义了路由何时到期。响应中将设置Sunset头部,如下所示:Sunset: Mon, 01 Jun 2020 00:00:00 GMT
  • enforce(可选)- 是一个布尔值,使路由在sunset日期之后不可访问(默认为false)。如果尝试访问设置此选项为true的路由,并且当前日期大于sunet日期,则会抛出GoneHttpException
  • name(可选)- 要弃用的路由名称。如果未指定,则将弃用该控制器上的所有路由。
  • deprecationDateTimeFormat(可选)- 用于弃用头部的日期时间的格式。默认为@U(根据IETF草案)。
  • sunsetDateTimeFormat(可选)- 用于日落头部的日期时间的格式。默认为D, d M Y H:i:s \G\M\T(根据日落RFC)。
  • deprecationLink(可选)- 用于弃用的Link头部中使用的链接。
  • sunsetLink - (可选)- 用于日落头部的Link头部中使用的链接。

您也可以使用路由参数弃用任何路由。

使用属性的示例

<?php

namespace App\Controller;

use HalloVerden\RouteDeprecationBundle\Attribute\DeprecatedRoute;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;


#[Route(path: self::PATH, name: self::NAME, methods: ['GET'])]
#[DeprecatedRoute(since: '2023-01-01')]
class GetHealthzController extends BaseController {
  const PATH = '/healthz';
  const NAME = 'healthz_get';

  /**
   * @return Response
   */
  public function __invoke(): Response {
    return new Response('ok');
  }

}

在routes.yaml中使用路由参数的示例(您还可以使用xml、php和路由注解)

# config/routes.yaml
lol_healthz:
    path: /healthz
    controller: App\Controller\GetHealthzController
    defaults:
        _deprecated_since: '2024-01-01'
        _deprecated_sunset: '2024-02-01'
        _deprecated_enforce: false
        _deprecated_deprecation_date_time_format: '@U'
        _deprecated_sunset_date_time_format: 'D, d M Y H:i:s \G\M\T'
        _deprecated_deprecation_link: 'https://example.com/deprecation'
        _deprecated_sunset_link: 'https://example.com/sunset'

配置选项(可选)

hallo_verden_route_deprecation:
    deprecation:
        dateTimeFormat: '@U'
        link: 'https://example.com/deprecation'
    sunset:
        dateTimeFormat: 'D, d M Y H:i:s \G\M\T'
        link: 'https://example.com/sunset'

贡献

欢迎提交拉取请求。对于重大更改,请首先提交问题以讨论您想要更改的内容。

请确保根据需要更新测试。

许可证

MIT