Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mtymek/blast-base-url
PSR-7 middleware and helpers for working with base URL.
https://github.com/mtymek/blast-base-url
Last synced: 3 months ago
JSON representation
PSR-7 middleware and helpers for working with base URL.
- Host: GitHub
- URL: https://github.com/mtymek/blast-base-url
- Owner: mtymek
- Created: 2015-12-20T15:42:11.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-11-29T22:14:16.000Z (about 1 year ago)
- Last Synced: 2024-10-12T20:37:40.160Z (3 months ago)
- Language: PHP
- Size: 27.3 KB
- Stars: 10
- Watchers: 3
- Forks: 8
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- awesome-http - mtymek/blast-base-url
README
Blast\BaseUrl
=============[![Build Status](https://travis-ci.org/mtymek/blast-base-url.svg?branch=master)](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 is supported using Composer:
```
$ composer require mtymek/blast-base-url
```If `Zend Component Installer` is present, it will automatically update application configuration.
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
Add `BaseUrlMiddleware` to your pipeline, just before routing middleware (`config/pipeline.php` file):
```php
// ...
$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:
```php
echo $request->getAttribute(BaseUrlMiddleware::BASE_URL);
// outputs: /some/subdirectory/index.phpecho $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.### 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
You will be able to use following syntax inside `zend-view` templates:
```html
```
Depending on your application directory, it will produce something similar to:
```html
```
#### Twig
You will be able to use following syntax inside `twig` templates:
```html
```
Depending on your application directory, it will produce something similar to:
```html
```