RemoveDevPrefixMiddleware.php
· 702 B · PHP
Raw
<?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));
}
}
| 1 | <?php |
| 2 | |
| 3 | use Interop\Http\ServerMiddleware\DelegateInterface; |
| 4 | use Interop\Http\ServerMiddleware\MiddlewareInterface; |
| 5 | use Psr\Http\Message\ServerRequestInterface; |
| 6 | |
| 7 | class RemoveDevPrefixMiddleware implements MiddlewareInterface |
| 8 | { |
| 9 | private $prefix; |
| 10 | |
| 11 | public function __construct(string $prefix = '/extra_directory') |
| 12 | { |
| 13 | $this->prefix = $prefix; |
| 14 | } |
| 15 | |
| 16 | public function process(ServerRequestInterface $request, DelegateInterface $delegate) |
| 17 | { |
| 18 | $uri = $request->getUri(); |
| 19 | $path = $uri->getPath(); |
| 20 | $path = preg_replace('#^' . $this->prefix . '#', '', $path); |
| 21 | |
| 22 | $uri = $uri->withPath($path); |
| 23 | return $delegate->process($request->withUri($uri)); |
| 24 | } |
| 25 | } |
pipeline.php
· 127 B · PHP
Raw
<?php
// Do this before piping the routing middleware
$app->pipe('/extra_path', new RemoveDevPrefixMiddleware('/extra_path'));
| 1 | <?php |
| 2 | |
| 3 | // Do this before piping the routing middleware |
| 4 | $app->pipe('/extra_path', new RemoveDevPrefixMiddleware('/extra_path')); |