sinso / app-routes
将类似REST的URL轻松路由到您的代码中
2.0.3
2024-05-15 11:46 UTC
Requires
- typo3/cms-core: ^11.0 || ^12.0
- dev-develop
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.5.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.0
- dev-topic/tsfe-v12
- dev-topic/tsfe-feuser
- dev-topic/drop-v10
- dev-topic/cache-reponse-header
- dev-bugfix/php-8-middleware
- dev-bugfix/multiilang-v10
- dev-bugfix/properly-init-tfse
- dev-task/code-cleanup
- dev-feature/response-cache
- dev-task/offload-constructor
This package is auto-updated.
Last update: 2024-09-24 09:11:08 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响应。 - 如果请求可以从缓存中提供服务,则不会调用您的请求处理程序。
- 只能缓存针对
GET
和HEAD
请求的响应。 - 缓存键由您的路由匹配的所有查询参数构建。
- 如果
$GLOBALS['TSFE']
参与了请求处理并且通过$tsfe->addCacheTags($tags)
添加了缓存标签,则这些标签将应用于缓存条目。 - 如果您启用了
$GLOBALS['TYPO3_CONF_VARS']['FE']['debug']
,HTTP响应将包含描述其缓存状态的标题。 - 具有
Cache-Control: no-cache
或Cache-Control: no-store
的响应不会缓存。 - 具有
Cache-Control: max-age=300
的响应将覆盖pages
缓存的默认TTL。