sinso/app-routes

将类似REST的URL轻松路由到您的代码中

安装次数: 65,974

依赖项: 1

建议者: 0

安全性: 0

星标: 21

关注者: 6

分支: 5

开放问题: 2

类型:typo3-cms-extension

2.0.3 2024-05-15 11:46 UTC

README

将任何URL路由到您的应用程序。

如果您想将某些URL直接路由到控制器,完全忽略TYPO3页面路由,则可以使用此包。
这特别适合创建REST API。

安装

composer req sinso/app-routes

配置

此包将在任何加载的扩展中查找Configuration/AppRoutes.yaml文件。创建此文件就是您开始所需的所有内容

myApp:
  prefix: /myApi/v2
  routes:
    - name: orders
      path: /orders
      defaults:
        handler: MyVendor\MyExtension\Api\OrdersEndpoint
    - name: order
      path: /order/{orderUid}
      defaults:
        handler: MyVendor\MyExtension\Api\OrderEndpoint

您提供的作为defaults.handler的类必须实现\Psr\Http\Server\RequestHandlerInterface。路由参数将在$request->getQueryParams()中可用。

选项

底层使用 symfony/routing

symfony/routing中作为YAML配置选项提供的所有内容都应该与此包直接兼容。

此包提供以下附加选项

  • defaults.cache: true - 如果为true,则响应将被缓存(请参阅以下详细信息)。(默认:false)
  • defaults.requiresTsfe: true - 如果为true,则在调用您的处理程序之前将初始化$GLOBALS['TSFE'](默认:false)。

生成路由URL

要生成URL,您可以使用Sinso\AppRoutes\Service\Router

$router = GeneralUtility::makeInstance(\Sinso\AppRoutes\Service\Router::class);
$url = $router->getUrlGenerator()->generate('myApp.order', ['orderUid' => 42]);
// https://www.example.com/myApi/v2/order/42

如果您需要在Fluid模板中生成URL,还有一个ViewHelper用于此目的。

<html
	xmlns:ar="http://typo3.org/ns/Sinso/AppRoutes/ViewHelpers"
	data-namespace-typo3-fluid="true"
>

{ar:route(routeName: 'myApp.order', parameters: {orderUid: '42'})}

</html>

配置模块

在配置模块中,有一个“App Routes”条目,显示所有配置的路由。需要TYPO3 v11

缓存

  • 可以通过配置defaults.cache: true来为每个路由启用缓存。
  • 使用TYPO3 pages缓存来缓存API响应。
  • 如果请求可以从缓存中提供服务,则不会调用您的请求处理程序。
  • 只能缓存针对GETHEAD请求的响应。
  • 缓存键由您的路由匹配的所有查询参数构建。
  • 如果$GLOBALS['TSFE']参与了请求处理并且通过$tsfe->addCacheTags($tags)添加了缓存标签,则这些标签将应用于缓存条目。
  • 如果您启用了$GLOBALS['TYPO3_CONF_VARS']['FE']['debug'],HTTP响应将包含描述其缓存状态的标题。
  • 具有Cache-Control: no-cacheCache-Control: no-store的响应不会缓存。
  • 具有Cache-Control: max-age=300的响应将覆盖pages缓存的默认TTL。