Skip to content
A URL base path detector for Slim 4
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
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
.gitattributes
.gitignore
.scrutinizer.yml Init Aug 21, 2019
.travis.yml Set minimum version to PHP 7.1 Aug 29, 2019
LICENSE
README.md Update README.md Sep 3, 2019
composer.json Set minimum version to PHP 7.1 Aug 29, 2019
phpcs.xml
phpstan.neon
phpunit.xml

README.md

selective/basepath

A URL base path detector for Slim 4.

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

Features

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

Supported servers

  • Apache webserver
  • PHP build-in webserver

Requirements

  • PHP 7.1+

Installation

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]

Usage

Detect the base path

use Selective\BasePath\BasePathDetector;

$basePathDetector = new BasePathDetector($_SERVER);

echo $basePathDetector->getBasePath();

Slim 4 integration

Example: public/index.php

<?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();
$app->setBasePath($basePath);

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

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

// Run app
$app->run();

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>
<html>
    <head>
        <meta charset="utf-8">
        <base href="{{ base_path }}"/>
    </head>
    <body>
    </body>
</html>

Middleware

  • TODO

License

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