Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uri is not decorated #213

Open
MrPropre opened this issue Aug 9, 2023 · 2 comments
Open

Uri is not decorated #213

MrPropre opened this issue Aug 9, 2023 · 2 comments

Comments

@MrPropre
Copy link

MrPropre commented Aug 9, 2023

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
Copy link
Member

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

@odan
Copy link
Contributor

odan commented Jul 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants