Customizing operation handler
Sometimes predefined operations may not be enough to handle all use cases. In fact build-in endpoints supports CRUD on Extbase entities. If you would like to create something more complex then you need to utilize custom operation handlers.
As name suggests, "operation handlers" determines how specific request is processed. T3api dispatcher checks if there is handler matching current request and executes it.
To register new operation handler it is needed to add new item to t3api configuration array, as on example below:
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['operationHandlers'][\Vendor\Extension\OperationHandler\MyCustomOperationHandler::class] = 100;
Name of the element of array is the name of the class and the value is the priority. Priority is needed because there may be multiple handlers which supports current request. Higher priority means that handler wins and will be executed. Only one handler will be executed for every request. Most of build-in handlers have negative priorities so it is suggested to use priority higher than 0 in custom handlers.
Important
At the moment there is one exception in core when build-in handler should be executed before any other: \Source. This is a handler used for preflight requests (OPTIONS) and it handles request according to CORS configuration. We do not exclude case of other core handlers which should be executed before the custom ones. All of them will have priority higher than 10000. It means suggested priority for custom handlers is higher than 0 and lower than 10000.
Every handler has to implement interface \Source. This interface forces handler class to contain two methods:
supports(static) - Basing on input arguments method takes a decision if current class can handle the operation and return boolean value (true- supports;false- does not support). Accepts arguments:-
$operation(\Source).Broker\ T3api\ Domain\ Model\ Operation Interface $request(\Symfony\).Component\ Http Foundation\ Request
handle- Contains code responsible for handling operation. Executed only whensupportsreturnstrue. Accepts arguments:-
$operation(\Source).Broker\ T3api\ Domain\ Model\ Operation Interface $request(\Symfony\).Component\ Http Foundation\ Request $route(array) - Array with route parameters (e.g.$routeinside single items operations which uses['id'] {id}in URL).&$response(\Psr\) - reference to response.Http\ Message\ Response Interface
Note
Please keep in mind that $response parameter of handle method is reference. It is needed to make it possible to customize response inside operation handler since response object is immutable. It means that new Response object has to be write to $response variable to really affect the server response. For example, if you would like to change the status code you should use such code inside your handler:
$response = $response ? $response->withStatus(201) : $response;
It is useful and advised (but not required) to extend existing abstract classes when creating custom handlers:
\Sourcewhen creating custom collection operation handler.Broker\ T3api\ Operation Handler\ Abstract Collection Operation Handler \Sourcewhen creating custom item operation handler.Broker\ T3api\ Operation Handler\ Abstract Item Operation Handler \Sourceor at least common abstract operation handler which contain bunch of useful things which may be needed in your custom handler (like deserialization process, injecting services or getting appropriate repository for current operation).Broker\ T3api\ Operation Handler\ Abstract Operation Handler
Even better to extend any of already existing operations if that is possible:
\SourceBroker\ T3api\ Operation Handler\ Collection Get Operation Handler \SourceBroker\ T3api\ Operation Handler\ Collection Post Operation Handler \SourceBroker\ T3api\ Operation Handler\ File Upload Operation Handler \SourceBroker\ T3api\ Operation Handler\ Item Delete Operation Handler \SourceBroker\ T3api\ Operation Handler\ Item Get Operation Handler \SourceBroker\ T3api\ Operation Handler\ Item Patch Operation Handler \SourceBroker\ T3api\ Operation Handler\ Item Put Operation Handler
Example
Example of declaration and usage of custom operation handler can be found in current user endpoint use case.