2017 © Pedro Peláez
 

library blast-base-url

PSR-7 middleware and helpers for working with base URL.

image

mtymek/blast-base-url

PSR-7 middleware and helpers for working with base URL.

  • Tuesday, May 15, 2018
  • by mtymek
  • Repository
  • 2 Watchers
  • 7 Stars
  • 10,537 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 6 Forks
  • 1 Open issues
  • 10 Versions
  • 12 % Grown

The README.md

Blast\BaseUrl

Build Status, (*1)

This package detects base URL of web application. It is useful when you need your app to be served from subdirectory (like http://localhost/my-project/public). This can be useful sometimes, especially in development environment., (*2)

View helpers for working with assets are also provided in the package., (*3)

Detection logic is based on zend-http package., (*4)

Installation

Installation is supported using Composer:, (*5)

$ composer require mtymek/blast-base-url

If Zend Component Installer is present, it will automatically update application configuration., (*6)

Usage

For simplicity, following instructions are targeting applications based on Zend Expressive Skeleton, assuming that Zend\ServiceManager was selected as DI container., (*7)

Blast\BaseUrl is based on PSR-7, so it will work well with other frameworks/dispatchers like Slim3 or Relay, just that wiring process will look different., (*8)

Base URL Middleware

Add BaseUrlMiddleware to your pipeline, just before routing middleware (config/pipeline.php file):, (*9)

// ...
$app->pipe(\Blast\BaseUrl\BaseUrlMiddleware::class);

// ...
$app->pipe(RouteMiddleware::class);

BaseUrlMiddleware will alter path from request URI, stripping base url. It means that even if you access your project from http:/localhost/~user/project/public/index.php/foo/bar, next middleware in the pipe will see the path as /foo/bar., (*10)

Additionally, two attributes will be added to ServerRequest, holding base URL and base path:, (*11)

echo $request->getAttribute(BaseUrlMiddleware::BASE_URL);   
// outputs: /some/subdirectory/index.php

echo $request->getAttribute(BaseUrlMiddleware::BASE_PATH);
// outputs: /some/subdirectory/

Generating URLs

BaseUrlMiddleware is able to automatically configure UrlHelper, so that all URLs generated by this helper will have appropriate prefix. This will be done automatically if UrlHelper is available in service container., (*12)

Accessing assets - base path

Another feature provided by this package is base path helper. It can be used to generate URLS for your asset files that work correctly under subdirectory., (*13)

If BasePathHelper is available, BaseUrlMiddleware will automatically configure it during execution., (*14)

Zend View

You will be able to use following syntax inside zend-view templates:, (*15)

<link rel="stylesheet" href="<?= $this->basePath('/css/style.css') ?>" />

Depending on your application directory, it will produce something similar to:, (*16)

<link rel="stylesheet" href="/public_html/my-project/public/css/style.css" />

Twig

You will be able to use following syntax inside twig templates:, (*17)

<link rel="stylesheet" href="{{ basePath('/css/style.css') }}" />

Depending on your application directory, it will produce something similar to:, (*18)

<link rel="stylesheet" href="/public_html/my-project/public/css/style.css" />

The Versions

15/05 2018

dev-php_72

dev-php_72

PSR-7 middleware and helpers for working with base URL.

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

middleware psr-7 psr7 expressive base url baseurl

26/06 2016

0.2.0

0.2.0.0

PSR-7 middleware and helpers for working with base URL.

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

middleware psr-7 psr7 expressive base url baseurl

01/06 2016

0.1.2

0.1.2.0

PSR-7 middleware and helpers for working with base URL.

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

middleware psr-7 psr7 expressive base url baseurl

25/01 2016

0.1.0

0.1.0.0

PSR-7 middleware and helpers for working with base URL.

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

middleware psr-7 psr7 expressive base url baseurl

25/01 2016

0.1.1

0.1.1.0

PSR-7 middleware and helpers for working with base URL.

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

middleware psr-7 psr7 expressive base url baseurl

25/01 2016

dev-use_zend_url_helper

dev-use_zend_url_helper

PSR-7 middleware and helpers for working with base URL.

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

middleware psr-7 psr7 expressive base url baseurl

21/12 2015

dev-base_path

dev-base_path

PSR-7 middleware and helpers for working with base URL.

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

middleware psr-7 psr7 expressive base url baseurl

21/12 2015

dev-unit_tests

dev-unit_tests

PSR-7 middleware and helpers for working with base URL.

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

middleware psr-7 psr7 expressive base url baseurl