Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Slim 4 - getBaseUrl middleware

Slim 4 - getBaseUrl middleware

It looks like the App::getBaseUrl method has been removed in Slim 4 (because it's not part of PSR-7).

As replacement you could use this Middleware to detect the baseUrl:

<?php

namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
 * Middleware.
 */
final class BaseUrlMiddleware implements MiddlewareInterface
{
    /**
     * The app base path.
     *
     * @var string
     */
    private $basePath;

    /**
     * The constructor.
     *
     * @param string $basePath The slim app basePath ($app->getBasePath())
     */
    public function __construct(string $basePath = '')
    {
        $this->basePath = $basePath;
    }

    /**
     * Invoke middleware.
     *
     * @param ServerRequestInterface $request The request
     * @param RequestHandlerInterface $handler The handler
     *
     * @return ResponseInterface The response
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $request = $request->withAttribute('base_url', $this->getBaseUrl($request));

        return $handler->handle($request);
    }

    /**
     * Return the fully qualified base URL.
     *
     * Note that this method never includes a trailing /
     *
     * @param ServerRequestInterface $request The request
     *
     * @return string The base url
     */
    public function getBaseUrl(ServerRequestInterface $request): string
    {
        $uri = $request->getUri();
        $scheme = $uri->getScheme();
        $authority = $uri->getAuthority();
        $basePath = $this->basePath;

        if ($authority !== '' && strpos($basePath, '/') !== 0) {
            $basePath .= '/' . $basePath;
        }

        return ($scheme !== '' ? $scheme . ':' : '')
            . ($authority ? '//' . $authority : '')
            . rtrim($basePath, '/');
    }
}

Setup

$app = AppFactory::create();

// optional
// $app->setBasePath($basePath);

// Register middleware
$app->addMiddleware(new BaseUrlMiddleware($app->getBasePath()));

// ...

$app->run();

Usage

$baseUrl = (string)$request->getAttribute('base_url'); // https://example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.