The documentation shows the ExampleBeforeMiddleware and the ExampleAfterMiddleware. Now I see that the ExampleBeforeMiddleware is not correct, but anyway I mean the ExampleAfterMiddleware.
Here is a PSR-15 middleware example:
<?php
namespace App\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
final class HttpLoggerMiddleware implements MiddlewareInterface
{
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger= $logger;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
// Add logging here...
$this->logger->info('Outgoing middleware example');
return $response;
}
}
I’m not sure what goal you need to achieve, but perhaps you could to perform async actions using a queue message service like RabbitMQ.
So, when you receive a request, you can send an async message with your data to a queue, and then send the response immediately.
In the meantime, in parallel, another worker/consumer program is listening in this queue, and when you receive a new message you process the data, do some extra stuff and finish your work flow.
I share an example of the documentation in case it is helpful.
You can install and integrate the php-amqplib client library in Slim projects using composer.