PHPackages                             mtymek/blast-base-url - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. mtymek/blast-base-url

ActiveLibrary[HTTP &amp; Networking](/categories/http)

mtymek/blast-base-url
=====================

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

1.0.0(8y ago)1054.1k↓71.1%8[2 issues](https://github.com/mtymek/blast-base-url/issues)[1 PRs](https://github.com/mtymek/blast-base-url/pulls)3BSD-2-ClausePHPPHP ^7.0CI failing

Since Dec 21Pushed 2y ago2 watchersCompare

[ Source](https://github.com/mtymek/blast-base-url)[ Packagist](https://packagist.org/packages/mtymek/blast-base-url)[ RSS](/packages/mtymek-blast-base-url/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (5)Dependencies (9)Versions (9)Used By (3)

Blast\\BaseUrl
==============

[](#blastbaseurl)

[![Build Status](https://camo.githubusercontent.com/be39d6b2937f919a962ed4e94699aeda76f6ebc8a49f9fbc43aa54d9722e799e/68747470733a2f2f7472617669732d63692e6f72672f6d74796d656b2f626c6173742d626173652d75726c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mtymek/blast-base-url)

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.

View helpers for working with assets are also provided in the package.

Detection logic is based on [`zend-http`](https://github.com/zendframework/zend-http)package.

Installation
------------

[](#installation)

Installation is supported using Composer:

```
$ composer require mtymek/blast-base-url

```

If `Zend Component Installer` is present, it will automatically update application configuration.

Usage
-----

[](#usage)

For simplicity, following instructions are targeting applications based on [Zend Expressive Skeleton](https://github.com/zendframework/zend-expressive-skeleton), assuming that `Zend\ServiceManager` was selected as DI container.

`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.

### Base URL Middleware

[](#base-url-middleware)

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

```
// ...
$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`.

Additionally, two attributes will be added to ServerRequest, holding base URL and base path:

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

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

### Generating URLs

[](#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.

### Accessing assets - base path

[](#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.

If `BasePathHelper` is available, `BaseUrlMiddleware` will automatically configure it during execution.

#### Zend View

[](#zend-view)

You will be able to use following syntax inside `zend-view` templates:

```
