Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Created September 14, 2017 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weierophinney/a8f3e77f4d227f2cabc61ec35c011565 to your computer and use it in GitHub Desktop.
Save weierophinney/a8f3e77f4d227f2cabc61ec35c011565 to your computer and use it in GitHub Desktop.
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