Handling Legacy Scripts with Header('Location: ') Calls

I’m using Slim as I go through the process of modernizing an old spaghetti code site. Previously, all of the .php scripts were just sitting in the document root. I’ve created a new public directory with my index.php, assets, etc and moved the rest of the scripts below the document root.

From there, the plan was use a catch-all route as a fallback while I gradually define new routes as I get things cleaned up and create the relevant controllers. My fallback route is dispatching to a LegacyController with a handle method which basically just includes the relevant legacy .php script. This initial attempt works in some cases, but the issue I’m stuck on at the moment is that some of these scripts have Header("Location: ...") calls in them to redirect after doing some logic, and I can’t think of a way to get this to work within the context of the Slim app lifecycle.

Wondering if anyone has any ideas? Please let me know if you need more info on what I’m trying to do.

Hi @ediv

I would try to add a custom middleware that does this:

  1. Start the output buffer with ob_start
  2. Invoke the next handler and take the $response object
  3. Optional: Fetch the output buffer with ob_get_contents()
  4. Clean the output buffer with ob_clean()
  5. Fetch all sent headers with the headers_list function
  6. Add these headers to the PSR-7 response object

Nice, thanks @odan! Your suggestion has me moving along with my solution.

1 Like