I am very happy to announce that the League URI toolkit for PHP developers 7.6 has been released!
If you’re new to the toolkit, you may want to start with its homepage to get an overview of what it can do for you. The toolkit is made up, currently, of four packages, each focused on making URI handling in PHP predictable, consistent, and developer-friendly.
This release includes many new features and improvements. Rather than detailing each change here—something the individual package CHANGELOG files already cover thoroughly—I’ll highlight a selection of the most notable additions and fixes:
PHP8.5
A new package, uri-polyfill, has been introduced to bring PHP’s new URI extension capabilities to older PHP versions, starting from PHP 8.1.
composer install league/uri-polyfill
On PHP 8.5+, the polyfill is not used—even if it is installed. The native PHP URI extension will be used automatically.
Support for PHP 8.5 has now landed across all URI packages. They work out of the box on the latest PHP version without deprecations and include seamless support for the new URI extension. The packages do not require the extension to function, but when it’s available, they will integrate with it automatically.
URI package
The league\uri package has now:
- a dedicated
League\Uri\Urnobject to improve support for URN; - the
League\Uri\UriTemplateclass can now generate PHP Native URI with URI resolution built-in; - URI validation has been improved with:
- full validation of
mailtoandblobURIs; - improved component-based validation for a broader range of scheme URI;
- full validation of
- The
League\Uri\Uriclass exposes more information around the URI and allows a range of new string representations.
While still present the BaseUri class is deprecated and will be removed from the package in the next major version. It is recommended to use the Uri class instead as most of its feature has been backported to it.
URI components package
The league/uri-components package introduces a new TextDirective class designed to enhance text-range highlighting within supported browsers.
The League\Uri\Modifier has been enhanced to support modification of the most widely used URI objects in the PHP ecosystem. It now works seamlessly with any PSR-7 URI implementation, PHP’s native Uri object, and League URI objects. With more than 60 modification methods available, it offers reliable, standard-compliant URI transformations with no loss of information.
Migration Guide
The documentation now reflects all the latest enhancements and new features. And because this isn’t a major release, you can enjoy the improvements without needing a migration guide.
Give Me Some Code
If everything I’ve explained so far feels too complex or hard to relate to, here’s a concrete example to show what’s now possible with the new versions:
First, require the new uri-polyfill package and require or update the uri-components package.
composer require league/uri-polyfill league/uri-components
By installing or upgrading the uri-components you are also installing/upgrading the uri package.
Then, copy/paste the following script locally:
use League\Uri\Components\FragmentDirectives\TextDirective;
use League\Uri\Modifier;
use League\Uri\UriTemplate;
require 'vendor/autoload.php';
$uriTemplate = new UriTemplate('{package}/{version}', ['version' => '7.0']);
$url = $uriTemplate->expandToUrl(['package' => 'polyfill'], 'https://uri.thephpleague.com');
// $url is a Uri\WhatWg\Url instance
$modifier = Modifier::wrap($url)
->addTrailingSlash()
->withFragment(new TextDirective(
start: "This package backports",
end: "PHP documentation website."
));
$newUri = $modifier->unwrap();
// $newUri is a Uri\WhatWg\Url instance
echo $newUri->toAsciiString(), PHP_EOL;
//we are calling a method on the Uri\WhatWg\Url
//if you are on PHP8.5 it will be the native object
//on PHP8.4- it will be the installed uri-polyfill
Finally, click or paste the generated link in your favorite browser!
Last but not least
Since this is a one-person project, if you notice anything that could be improved in the package or its documentation, please consider opening an issue or submitting a pull request. The URI packages are open-source under the MIT License, and contributions of all kinds are welcome and fully credited. Whether it’s reporting a bug, requesting or implementing a missing feature, fixing a typo, improving the documentation, or sponsoring the project, your support makes a real difference.
Happy coding!