A new URI handling library for PHP.
The last little while have been all about URI’s for me. I’ve been engaged in discussions about the upcoming ‘PSR-7’, I started working on a sort of ‘hypermedia’-focussed router/repository type of system, and I recently ran into a bug in sabredav that forced me to get a better understanding of the URI specification, in particular on the topics of ‘normalization’ and resolving relative URLs.
All of this accumulated in a set of functions of operations I commonly need, which I’m now releasing as an open source library.
Yup! Yet another URI library. All this library is though, is a collection of 5 php functions:
resolveto resolve relative uris.
normalizeto aid in comparing uris.
parse, which mostly works like PHP’s parse_url.
buildto do the exact opposite of
splitto easily get the ‘dirname’ and ‘basename’ of a URL without all the problems those two functions have.
The bug I ran into with sabredav, had to do with a client using a different
encoding of a URI than I expected to. After diving into rfc3986, I
realized that an application comparing uris cannot simply compare using
For example, the following uris are all identical:
normalize function can take any of these previous URIs, and will
turn it into the first.
<?php use Sabre\Uri; $input = 'HTTP://example.ORG:80/bar/./../%7efoo/'; echo Uri::normalize($input); // output: http://example.org/~foo/ ?>
I hope it’s useful for others.