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.
composer req sinso/app-routesThis 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\OrderEndpointThe class you provide as defaults.handler has to implement \Psr\Http\Server\RequestHandlerInterface.
The routing parameters will be available in $request->getQueryParams().
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.requiresTypoScript: true- If true, then thefrontend.typoscriptrequest attribute will be initialized before your handler is called (default:false).
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/42If 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>In the configuration module there's an entry "App Routes", that shows all configured routes.
- Caching can be enabled per route via configuration
defaults.cache: true. - The TYPO3
pagescache 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
GETandHEADrequests can be cached. - The cache key is built from all query parameters that were matched by your route.
- Any cache tags added to TYPO3's
CacheDataCollector($request->getAttribute('frontend.cache.collector')) 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-cacheorCache-Control: no-storeare not cached. - Responses with
Cache-Control: max-age=300overwrite the default TTL of thepagescache.
Setting an ETag header in your response will enable conditional requests, i.e. the client doesn't need to download the response body if it already has the latest version.
- If your response contains an
ETagheader and it matches theIf-None-Matchheader of the request, the response HTTP status will be304 Not Modifiedand the response body will be empty.