Germania KG · UrlPrefixer
Prefix any URL with a base URL, if not absolute or hashtagged “jump link”., (*1)
, (*2)
Installation with Composer
$ composer require germania-kg/urlprefixer
Usage
<?php
use Germania\UrlPrefixer\UrlPrefixer;
// Your prefix, normally a 'base URL'
$prefixer = new UrlPrefixer( 'https://myapp.com/project' );
echo $prefixer('/index.html');
// Result: "https://myapp.com/project/index.html"
// If URL is absolute, nothing happens:
echo $prefixer('//dist/styles.css');
// Result: "//dist/styles.css"
// If URL is hashtagged, nothing happens as well:
echo $prefixer('#navigation');
// Result: "#navigation"
Custom URL prefixes
You may pass a second parameter to the Callable to override the default prefix you set with on instantiation:, (*3)
<?php
// Your prefix, normally a 'base URL'
$prefixer = new UrlPrefixer( 'https://myapp.com/project' );
echo $prefixer('/logo.jpg', 'https://cdn.test.com');
// Result: "https://cdn.test.com/logo.jpg"
// Like above, nothing happens when URL is absolute:
echo $prefixer('//dist/styles.css', 'https://cdn.test.com');
// Result: "//dist/styles.css"
Issues
See issues list., (*4)
Development
$ git clone https://github.com/GermaniaKG/UrlPrefixer.git
$ cd UrlPrefixer
$ composer install
Unit tests
Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:, (*5)
$ composer test
# or
$ vendor/bin/phpunit