Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstrates stripping a path prefix prior to routing.
<?php
// Do this before piping the routing middleware
$app->pipe('/extra_path', new RemoveDevPrefixMiddleware('/extra_path'));
<?php
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
class RemoveDevPrefixMiddleware implements MiddlewareInterface
{
private $prefix;
public function __construct(string $prefix = '/extra_directory')
{
$this->prefix = $prefix;
}
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$uri = $request->getUri();
$path = $uri->getPath();
$path = preg_replace('#^' . $this->prefix . '#', '', $path);
$uri = $uri->withPath($path);
return $delegate->process($request->withUri($uri));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.