Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/selective-php/basepath
Base path detector for Slim 4
https://github.com/selective-php/basepath
apache basepath php slim slim4
Last synced: 3 months ago
JSON representation
Base path detector for Slim 4
- Host: GitHub
- URL: https://github.com/selective-php/basepath
- Owner: selective-php
- License: mit
- Created: 2019-08-21T08:38:23.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-09-09T13:26:56.000Z (about 1 year ago)
- Last Synced: 2024-07-23T14:45:50.625Z (4 months ago)
- Topics: apache, basepath, php, slim, slim4
- Language: PHP
- Homepage:
- Size: 40 KB
- Stars: 47
- Watchers: 3
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-slim - BasePath - Base path detector for Slim 4. (Miscellaneous)
README
# selective/basepath
A URL base path detector for Slim 4.
[![Latest Version on Packagist](https://img.shields.io/github/release/selective-php/basepath.svg)](https://packagist.org/packages/selective/basepath)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)
[![Build Status](https://github.com/selective-php/basepath/workflows/build/badge.svg)](https://github.com/selective-php/basepath/actions)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/selective-php/basepath.svg)](https://scrutinizer-ci.com/g/selective-php/basepath/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/quality/g/selective-php/basepath.svg)](https://scrutinizer-ci.com/g/selective-php/basepath/?branch=master)
[![Total Downloads](https://img.shields.io/packagist/dt/selective/basepath.svg)](https://packagist.org/packages/selective/basepath/stats)## Features
* Support for Apache and the PHP built-in webserver
* Tested
* No dependencies
* Very fast### Supported servers
* Apache webserver with mod_rewrite and .htaccess
* PHP build-in webserver## Requirements
* PHP 7.2+ or 8.1+
## Installation
```
composer require selective/basepath
```The recommended **directory structure**:
* `public/` Web server files, the [DocumentRoot](https://httpd.apache.org/docs/2.4/de/mod/core.html#documentroot)
* `.htaccess` Apache redirect rules for the front controller
* `index.php` The front controller
* `.htaccess` Internal redirect to the public/ directoryThe 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:
```htaccess
# 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 second `.htaccess` file above the `public/` directory with this content:
```htaccess
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
```## Usage
### Slim 4 integration
Add the `BasePathMiddleware` after `addRoutingMiddleware()` to set the basePath before
the routing is started.Example: `public/index.php`
```php
addRoutingMiddleware();// Set the base path to run the app in a subdirectory.
// This path is used in urlFor().
$app->add(new BasePathMiddleware($app));$app->addErrorMiddleware(true, true, true);
// Define app routes
$app->get('/', function (Request $request, Response $response) {
$response->getBody()->write('Hello, World!');
return $response;
})->setName('root');// Run app
$app->run();
```### Apache usage
* Start the apache webserver
* Open your website, e.g. `http://localhost` or `http://localhost/{my-sub-directory}` and you should see the message `Hello, World!`.### PHP built-in webserver usage
* Open the console and change into the project `public/` directory. Then run:
```
php -S localhost:8000
```If you don't start the webserver from the project `public/` directory, you have start it with a specific document root directory:
```
php -S localhost:8000 -t public
```* Open `http://localhost:8000` and you should see the message `Hello, World!`.
### Good URLs
The `public/` directory is only the `DocumentRoot` of your webserver,
but it's never part of your base path and the official url.Good URLs:
* `https://www.example.com`
* `https://www.example.com/users`
* `https://www.example.com/my-app`
* `https://www.example.com/my-app/users`Bad URLs:
* `https://www.example.com/public`
* `https://www.example.com/public/users`
* `https://www.example.com/my-app/public`
* `https://www.example.com/my-app/public/users`### Retrieving the base path
```php
$basePath = \Slim\Routing\RouteContext::fromRequest($request)->getBasePath();
```### Creating a relative url with the base path
```php
$routeParser = \Slim\Routing\RouteContext::fromRequest($request)->getRouteParser();
$url = $routeParser->urlFor('root');
```### Rendering the base path into a Twig layout template
This example requires [slim/twig-view](https://github.com/slimphp/Twig-View)
```twig
{% block content %}{% endblock %}