slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.94k stars 1.95k forks source link

UrlFor protocol #3332

Closed tobiascapin closed 2 months ago

tobiascapin commented 2 months ago

I'm using urlFor method to get the full URL of a route, but is it not clear to me if it is possibile to set the wanted protocol, it seems always http is given.

tobiascapin commented 2 months ago

Sorry, I just discovered the current UriInterface is used and my server uses http because https is handled by the balancer.

odan commented 2 months ago

The schema of the request object could be switched using a middleware that you add after the RoutingMiddleware.

$app->addRoutingMiddleware();

$app->add(function(ServerRequestInterface $request, RequestHandlerInterface $handler) {
    $request = $request->withUri($request->getUri()->withScheme('https'));
    return $handler->handle($request);
});

To get the full url you can use the fullUrlFor method of the RouteParser instance.

// https://api.example.com/
$fullUrl = $routeParser->fullUrlFor($request->getUri(), 'home', $data, $queryParams);
tobiascapin commented 2 months ago

Great. Thank you very much.

tobiascapin commented 5 hours ago

I come back to this topic because it seems getUri()->withScheme('https') doesn't fill the correct port. had also to add ->withPort(443) otherwise some https URL will be made like https://myhost:80/path and doesn't work. I don't know if I'm missing something or by design withScheme has to go with withPort method.