slimphp / Slim-Http

A set of PSR-7 object decorators providing useful convenience methods
MIT License
151 stars 47 forks source link

Uri is not decorated #213

Open MrPropre opened 1 year ago

MrPropre commented 1 year ago

Hello,

I wanted to use Uri::getBaseUrl from the decorated UriInterface but it is not defined. When I use $request->getUri(), the returned object is an instance of Slim/Psr7/Uri, I think it should be Slim/Http/Uri instead.

dump($request, $request->getUri());

image

Thank you in advance !

l0gicgate commented 11 months ago

Please show a reproducible example of this with code with a minimal app setup.

odan commented 2 months ago

I can confirm that issue. Here is a minimal app:

<?php

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\UriInterface;
use Slim\Factory\AppFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) {
    $response->getBody()->write('Hello world!');

    return $response;
});

$app->add(ExampleMiddleware::class);

final class ExampleMiddleware implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        $response = $handler->handle($request);

        // Fatal error: Uncaught Error: Call to undefined method Slim\Psr7\Uri::getBaseUrl()
        return $response->withJson(['url' => $request->getUri()->getBaseUrl()]);
    }
}

$app->run();

Error message:

Fatal error: Uncaught Error: Call to undefined method Slim\Psr7\Uri::getBaseUrl()

The request and the response object is decorated, but the Uri is not decorated.

var_dump($request); // Slim\Http\ServerRequest

$response = $handler->handle($request);
var_dump($response); // Slim\Http\Response

$uri = $request->getUri();
var_dump($uri); // Slim\Psr7\Uri