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 nottext/css
. I am not sure why, because in the main Mezzio app css files are loading just fine. Maybe it is an artifact ofinclude
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:
- 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? Maybeinclude
is not the right way for me to include files? - 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 believeinclude
directive does not work for URL parameters