Slim Framework 4: process request manually

691 Views Asked by At

I'm developing Web API with Slim Framework 4. Nothing special, everything as usual. Request handlers are something like:

$app = \Slim\Factory\AppFactory::create();
...
$app->get('/resource', function($request, $response)
{
    $response->getBody()->write(json_encode(/* some data */));
    return $response->withHeader('Content-Type', 'application/json');
});
...
$app->run();

However at some point inside one of handlers I need to invoke my own API method and get its result. For example: assume there is a special API method for batch invocation. It accepts a list of GET resources and responds with an array of results where every element of that array is a result of API invocation for corresponding API URL:

$app->post('/batch', function($request, $response)
{
    $urls = $request->getParsedBody();

    $results = [];

    foreach($urls as $url)
    {
        $results[] = /* invoke my own API with method = GET and uri = $url, and push result into array */
    }

    $response->getBody()->write(json_encode($results));
    return $response->withHeader('Content-Type', 'application/json');
});

This should be done somehow with the same Slim App instance. However I couldn't find anything about that in Slim documentation. Currently I'm researching Slim source code, but may be you guys already know the answer?

0

There are 0 best solutions below