Created
September 14, 2017 19:23
-
-
Save weierophinney/a8f3e77f4d227f2cabc61ec35c011565 to your computer and use it in GitHub Desktop.
Demonstrates stripping a path prefix prior to routing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Do this before piping the routing middleware | |
$app->pipe('/extra_path', new RemoveDevPrefixMiddleware('/extra_path')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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