All Projects → selective-php → basepath

selective-php / basepath

Licence: MIT license
Base path detector for Slim 4

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to basepath

slim-tutorial
Php Slim Framework Tutorial
Stars: ✭ 26 (-27.78%)
Mutual labels:  slim, slim4
Slim-Auth
A Slim 4 Skeleton.
Stars: ✭ 22 (-38.89%)
Mutual labels:  slim, slim4
Elliot
Comprehensive and Rigorous Framework for Reproducible Recommender Systems Evaluation
Stars: ✭ 49 (+36.11%)
Mutual labels:  slim
Tf featureextraction
Convenient wrapper for TensorFlow feature extraction from pre-trained models using tf.contrib.slim
Stars: ✭ 169 (+369.44%)
Mutual labels:  slim
Slim Oauth2
Routes and Middleware for Using OAuth2 Server within a Slim Framework API
Stars: ✭ 121 (+236.11%)
Mutual labels:  slim
Slimvalidation
A validator for PHP with Respect/Validation
Stars: ✭ 62 (+72.22%)
Mutual labels:  slim
Slim
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
Stars: ✭ 11,171 (+30930.56%)
Mutual labels:  slim
Authorize Slim 4
Slim 4 Authorization Tutorial
Stars: ✭ 39 (+8.33%)
Mutual labels:  slim
Slim4 Skeleton
A Slim 4 Skeleton
Stars: ✭ 237 (+558.33%)
Mutual labels:  slim
Mobilenet
MobileNet build with Tensorflow
Stars: ✭ 1,531 (+4152.78%)
Mutual labels:  slim
Assetchecker
👮Sanitize your Assets.xcassets files
Stars: ✭ 167 (+363.89%)
Mutual labels:  slim
Machdas
Todo manager based on PHP 7, Slim and Vue.js
Stars: ✭ 78 (+116.67%)
Mutual labels:  slim
Slim3
Slim Framework 3 Skeleton Application
Stars: ✭ 70 (+94.44%)
Mutual labels:  slim
Slim themes
A Beautiful Collection Of SLiM Themes.
Stars: ✭ 148 (+311.11%)
Mutual labels:  slim
Bottyclient
A slim Discord client with many cool features including less network traffic which supports bot tokens, but user tokens theoretically work too. Tags: Discord Bot Client Token for Bot Botting Download English
Stars: ✭ 58 (+61.11%)
Mutual labels:  slim
Slim Born
Slim Framework 3 and 4 skeleton application has authentication MVC construction.
Stars: ✭ 179 (+397.22%)
Mutual labels:  slim
Seg Mentor
TFslim based semantic segmentation models, modular&extensible boutique design
Stars: ✭ 43 (+19.44%)
Mutual labels:  slim
Slim Swoole
Convenient library to run SlimPHP applications with Swoole
Stars: ✭ 75 (+108.33%)
Mutual labels:  slim
Dry View
Complete, standalone view rendering system that gives you everything you need to write well-factored view code.
Stars: ✭ 124 (+244.44%)
Mutual labels:  slim
Fengniao
A command line tool for cleaning unused resources in Xcode.
Stars: ✭ 2,852 (+7822.22%)
Mutual labels:  slim

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 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.0+

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 second .htaccess file above the public/ directory with this content:

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

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

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

$app = AppFactory::create();

// Add Slim routing middleware
$app->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

$basePath = \Slim\Routing\RouteContext::fromRequest($request)->getBasePath();

Creating a relative url with the base path

$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

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <base href="{{ base_path() }}/"/>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

Support

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].