Capture slim response after $app->run()

Hi, I’m migrating an app away from SlimPHP to Laravel (sorry!)

I’ve been following the approach described here: https://tighten.com/insights/converting-a-legacy-app-to-laravel/

The SlimPHP app is wrapped by Laravel to allow gradual migration. Laravel forwards any request not matching a Laravel route on to the SlimPHP code, and uses output buffering to capture the reponse.

public function __invoke()
{
    ob_start();
    require app_path('Http') . '/legacy.php';
    $output = ob_get_clean();
 
    return new Response($output);
}

But this ignores things like response headers, status codes etc.

Since the SlimPHP route is returning a PSR-7 Response it feels I ought to be able to capture the headers, status codes and status text somehow at least, if not just directly return the response object itself.

If anyone has any ideas on this I’d really appreciate any suggestions. Thanks.

Answered my own question.

Don’t know why I didn’t think of this at first. I don’t use these ‘raw’ PHP functions very often I suppose. I dipped into the Slim source to figure out what $app->run(); was really doing, and realised I could just read the status code and headers directly and include them in the laravel response.

public function __invoke(): Response
    {
        ob_start();
        require app_path('Http') . '/legacy.php';
        $output = ob_get_clean();

        return response($output, http_response_code())->withHeaders(headers_list());
    }