Skip to content
A URL base path detector for Slim 4
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Init Aug 21, 2019
tests Init Aug 21, 2019
.cs.php Init Aug 21, 2019
.scrutinizer.yml Init Aug 21, 2019
.travis.yml Set minimum version to PHP 7.1 Aug 29, 2019
LICENSE Update Sep 3, 2019
composer.json Set minimum version to PHP 7.1 Aug 29, 2019


A URL base path detector for Slim 4.

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads


  • Support for multiple servers
  • Tested
  • No dependencies
  • Very fast

Supported servers

  • Apache webserver
  • PHP build-in webserver


  • PHP 7.1+


composer require selective/basepath

The recommended directory structure:

  • public/ Web server files, the DocumentRoot
    • .htaccess Apache redirect rules for the front controller
    • index.php The front controller
  • .htaccess Internal redirect to the public/ directory

The following steps are necessary for your Slim 4 application:

For Apache we have to "redirect" the web traffic to the front controller in public/index.php.

Create a file: public/.htaccess with this content:

# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

We also need a rule to "redirect" the sub-directories to the front-controller in public/index.php.

Create a .htaccess file above the public/ directory with this content:

RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]


Detect the base path

use Selective\BasePath\BasePathDetector;

$basePathDetector = new BasePathDetector($_SERVER);

echo $basePathDetector->getBasePath();

Slim 4 integration

Example: public/index.php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

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

$app = AppFactory::create();

// Set the base path to run the app in a subdirectory.
// This path is used in urlFor().
$basePath = (new BasePathDetector($_SERVER))->getBasePath();

// Add middleware
$app->addErrorMiddleware(true, true, true);

// Define app routes
$app->get('/', static function (Request $request, Response $response) {
    $response->getBody()->write('Hello, World!');
    return $response;

// Run app

Creating a relative url with a base path using the RouteParser:

$routeParser = $app->getRouteCollector()->getRouteParser();
echo $routeParser->urlFor('root');

Passing the base path into all Twig templates using the App instance:

$environment->addGlobal('base_path', $app->getBasePath() . '/');

Passing the base path into all Twig templates using a route:

$routeParser = $app->getRouteCollector()->getRouteParser();
$twig->addGlobal('base_path', $routeParser->urlFor('root'));

Twig template example:

<!DOCTYPE html>
        <meta charset="utf-8">
        <base href="{{ base_path }}"/>


  • TODO


  • MIT
You can’t perform that action at this time.