Getting started
What does it do
T3api provides a configurable and customizable REST API for Extbase models. API resources, their operations, filters and access rules are configured with annotations on the model class (or on a class extending a model of another extension, see Integration), output of the models with annotations or YAML serializer metadata.
Most configuration options follow API Platform, so they are familiar to developers who know this framework.
Responses use JSON-LD and Hydra (partial support), which allows frontend applications to discover resources, filters and pagination from the responses.
Installation
Supported versions:
| TYPO3 | PHP |
|---|---|
| 12.4 | 8.1 - 8.4 |
| 13.4 | 8.2 - 8.5 |
| 14 | 8.2 - 8.5 |
Run
composer require sourcebroker/t3api
Configuration
Route enhancer
Import route enhancer by adding following lines at the bottom of your site config.:
imports:
- { resource: "EXT:t3api/Configuration/Routing/config.yaml" }
If you do not want to use import you can also manually add new route enhancer of type T3api directly
in your site configuration.
routeEnhancers:
T3api:
type: T3apiResourceEnhancer
Default base path to api requests is: _api. To change it, it is needed to extend route enhancer configuration by
base property, as in example below:
routeEnhancers:
T3api:
type: T3apiResourceEnhancer
basePath: 'my_custom_api_basepath'
Creating API resource
Next step is to make an API resource from our entity.
To map Extbase model to API resource it is just needed to add @Source annotation
to our model class and define at least one operation (endpoint):
use SourceBroker\T3api\Annotation\ApiResource;
/**
* @ApiResource(
* collectionOperations={
* "get"={
* "path"="/items",
* },
* },
* itemOperations={
* "get"={
* "path"="/items/{id}",
* }
* },
* )
*/
class Item extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
}
After flushing caches the items are available under https:// and single item under https://.
Note
By default all properties of the entity are returned. Use serialization context groups to control which properties are exposed.
Note
API resource can be created only from class which:
- Extends
\TYPO3\.CMS\ Extbase\ Domain Object\ Abstract Domain Object - Is kept in path
EXT:(can be changed, see Api Resource Path).{extkey}/ Classes/ Domain/ Model - Exists in enabled extension.
Real examples. Run "ddev restart && ddev ci 14" and try those links below.
Testing instance uses EXT: which exposes EXT: records as API resources.
Backend module
Backend module Web API shows the API of a site in Swagger UI - all resources and operations with their parameters, filters and response schemas, generated as OpenAPI specification from the configuration of the API resources. Select the site in the module header. The site needs the route enhancer, otherwise the module shows an error message.
Backend users need access to the module (admins have it by default).
Important
"Try it out" in Swagger UI sends real requests to the API of the selected site - POST, PATCH, PUT and
DELETE change and delete records. The browser sends the requests with its cookies of the API domain, so
security expressions see the frontend or backend user logged in on that domain.
The backend Content Security Policy of the module allows requests to the domains of all sites with the t3api route enhancer. Swagger UI does not use the online validator of swagger.io, so the URL of the specification is not sent outside of the TYPO3 instance.