sinso / app-routes
Easy way to route rest-like URLs to your code
Installs: 74 896
Dependents: 1
Suggesters: 0
Security: 0
Stars: 22
Watchers: 6
Forks: 5
Open Issues: 3
Type:typo3-cms-extension
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/v13
- dev-topic/request-cache
- 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-12-04 10:47:48 UTC
README
Route any URL to your application.
You use this package if you want to route certain URLs directly to your controllers, completely ignoring the TYPO3 page routing.
This is especially useful to create REST APIs.
Installation
composer req sinso/app-routes
Configuration
This package will look for Configuration/AppRoutes.yaml
files in any loaded extension. Creating this file is all you need to get started:
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
The class you provide as defaults.handler
has to implement \Psr\Http\Server\RequestHandlerInterface
.
The routing parameters will be available in $request->getQueryParams()
.
Options
Under the hood symfony/routing is used.
Everything that is available as YAML configuration option in symfony/routing
should work with this package out of the box.
This package offers these additional options:
defaults.cache: true
- If true, then responses are cached (see more details below). (default:false
)defaults.requiresTsfe: true
- If true, then$GLOBALS['TSFE']
will be initialized before your handler is called (default:false
).
Generate Route URLs
To generate URLs you can use the 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
If you need to generate a URL in a Fluid template, there's also a ViewHelper for that:
<html xmlns:ar="http://typo3.org/ns/Sinso/AppRoutes/ViewHelpers" data-namespace-typo3-fluid="true" > {ar:route(routeName: 'myApp.order', parameters: {orderUid: '42'})} </html>
Configuration Module
In the configuration module there's an entry "App Routes", that shows all configured routes. Requires TYPO3 v11
Caching
- Caching can be enabled per route via configuration
defaults.cache: true
. - The TYPO3
pages
cache is used to cache API responses. - Your request handler will not be called at all if the request can be served from cache.
- Only responses for
GET
andHEAD
requests can be cached. - The cache key is built from all query parameters that were matched by your route.
- If
$GLOBALS['TSFE']
was involved in handling the request and cache tags were added to it via$tsfe->addCacheTags($tags)
, those are applied to the cache entry. - If you have
$GLOBALS['TYPO3_CONF_VARS']['FE']['debug']
enabled, the HTTP response contains headers describing its cache status. - Responses with
Cache-Control: no-cache
orCache-Control: no-store
are not cached. - Responses with
Cache-Control: max-age=300
overwrite the default TTL of thepages
cache.