-
-
Notifications
You must be signed in to change notification settings - Fork 13
Description
Feature Request
| Q | A |
|---|---|
| New Feature | yes |
| RFC | no |
| BC Break | no |
Summary
In mezzio/mezzio#69, the author requested a built-in feature for mezzio to allow the application be stored in a sub-directory.
This brings several issues, as the router and all tooling around it uses the incoming request path to match routes. So either, the routes are provided with the sub-directory in-place or something will interact with the request and removes the pre-configured base path (subdirectory) from the incoming request.
I've scribbled down my idea to do this during runtime in case someone does not have access to the webserver configuration and thus cannot add any rewrite rules to it.
WARNING: It is highly recommended to use the webservers configuration to ensure proper document roots and to block access to vendor/, config/, data/, e.g. directories!
final class RemoveBasePathMiddleware implements MiddlewareInterface
{
private string $basePath;
public function __construct(string $basePath)
{
$this->basePath = $basePath;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return $handler->handle($this->removeBasePathFromRequest($request));
}
private function removeBasePathFromRequest(ServerRequestInterface $request): ServerRequestInterface
{
$uri = $request->getUri();
return $request->withUri(
$uri->withPath(
(string) \preg_replace(sprintf('!^%s!', $this->basePath), '', $uri->getPath())
)
);
}
}I think this middleware could be part of this package and thus adding the feature request here.
When we have this middleware in-place, @froschdesign brought up the idea to integrate a new installation step to the mezzio-skeleton. To make this an easy installation step, we might provide a laminas-cli command in here (or in the mezzio-tooling component, but I prefer to have it in the same component as the middleware itself), so it can be easily activated/deactivated. Thanks for that amazing idea, Frank!