Manipulate (e.g. sanitize) incoming request parameter before deserialize-operation #134
-
Is there a way to manipulate incoming request parameter in a custom operation-handler, before the deserialize-operation is called? For example to sanitize certain input values, similar to what we can do in Extbase's The Use-case: In an Extbase controller we could solve this like this:
Is there something similar we can do in T3API? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
t3api uses PSR-15 middleware to generate the output. You could create your own middleware to modify the query parameter for your endpoint. Try something like this: <?php
namespace Vendor\YourExtension\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use SourceBroker\T3api\Service\RouteService;
class ApiManipulation implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// check for t3api requests
if (!RouteService::routeHasT3ApiResourceEnhancerQueryParam($request)) {
return $handler->handle($request);
}
// or check if the request is for a specific path with $request->getUri()->getPath()
$queryParams = $request->getQueryParams();
if (isset($queryParams['size'])) {
$queryParams['size'] = str_replace('.', '', $queryParams['size']);
$request = $request->withQueryParams($queryParams);
}
return $handler->handle($request);
}
} Make sure to register the middleware between the two middlewares of t3api:
<?php
return [
'frontend' => [
'vendor/your-extension' => [
'target' => \Vendor\YourExtension\Middleware\ApiManipulation::class,
'before' => [
'sourcebroker/t3api/process-api-request',
],
'after' => [
'sourcebroker/t3api/prepare-api-request',
],
],
],
]; |
Beta Was this translation helpful? Give feedback.
t3api uses PSR-15 middleware to generate the output. You could create your own middleware to modify the query parameter for your endpoint. Try something like this: