Legacy app issue with loading URL parameters and incorrectly setting CSS mime type

I am having an issue with setting up access to a legacy app via Mezzio.

First, here is my set up:

class LegacyApplicationFactory
{

    public function __invoke(ContainerInterface $container): MiddlewareInterface
    {
        // I put my legacy app as-is into the "<APP_DIR>/portal" directory
        // and chdir this seems to work to load legacy app's includes, 
        // requires, and other directives
        chdir('portal');
        return new LegacyApplicationMiddleware();
    }
}

and Middleware:

class LegacyApplicationMiddleware implements MiddlewareInterface
{

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        try
        {
            ob_start();
            // Requested URL: http://app/portal/portal.php?p=selector
            $path = $request->getUri()->getPath(); // "/portal/portal"
            $path = substr($path, 8); // "portal/portal"
            include $path; //loads "login" page okay but without CSS stylesheets

            $output = ob_get_contents();
            ob_end_clean();

            return new HtmlResponse($output);
        }
        catch (Throwable $exception)
        {
            return $handler->handle($request);
        }
    }
}

with the above I am able to get my legacy app to show up! However there are some issues.

Issues are:

  • css files are not loading correctly. Even though they are being loaded, they are loaded with text/html mime type, and not text/css. I am not sure why, because in the main Mezzio app css files are loading just fine. Maybe it is an artifact of include directive? The code that runs CSS is <link rel="stylesheet" href="css/login.css" type="text/css" />, and I’ve tried adding various Apace directives suggested on StackOverflow for .css to no avail. The only thing that seems to work is to add this to the first line of CSS file: <?php header("Content-Type:text/css; charset: UTF-8"); ?>
  • I am not loading URL parameters, i.e. ?p=selector

Questions:

  1. Middleware-code-wise is this the best possible way to code up the legacy app access for my case? Is using include the right way to go? Is there a better, or a more Mezzio-native way to do the same? Has anyone encountered the above CSS issue before? Maybe include is not the right way for me to include files?
  2. How do I load URL parameters, i.e. in my code above I am currently ignoring the URL parameters of ?p=selector. I must include it in the request, but I believe include directive does not work for URL parameters

Hi Dennis

Instead of the chdir() you could either use set_include_path() somewhere in the bootstrapping of your app, which tells PHP where to look for the required files. Or add a classmap to Composer which will make manuel requiring files unnecessary as Composer will autoload them.

To somewhat answer both questions: What you (partially) solved manually, usually is solved by a router. Have a look at routing in Mezzio. The routing components provide battle tested functionality and will most likely provide what you need as well.

For the incorrect header type for css assets: How are these files loaded? From the information at hand it looks like they are shipped through the legacy PHP app and not directly by the web server?

Thanks. I am trying to move a legacy application with hundreds of existing legacy hardcoded paths.
When you are suggesting to look at routing in Mezzio, are you saying that I will need to rewrite all those paths to be under the new routing system?

Sorry I’m getting a bit lost. There are quite a few tutorials how to reuse/repurpose a ZF1 application in Mezzio, but so far no simple 2000-era “old school” application in Mezzio.

What I have is the 2000-era application where routing is URL-to-file. I am lost as to how to inject mezzio Routing in between that url-to-file routing.

With hundreds of endpoints I would not bother (manually) creating those routes, no. I would probably refine the LegacyApplicationMiddleware to properly serve requested routes as you have already started (plus request parameters and what not).