Handling URLs with League\URL

Attention: Les informations de ce billet sont susceptibles d'être obsolètes car vieux de plus 2 ans.

Warning: The information you are reading may be obsolete, this post was published more than 2 years ago.

While moving my CSV library over to thephpleague group I happened to also move another simple library that I had built earlier called URL. As the name implied it was an attempt to ease URL manipulation in PHP.

A basic PHP installation comes with 3 powerful functions:

  • http_build_query to transform an array into a query string
  • parse_str that does the opposite and given a query string transforms it into a PHP array
  • And last but not least parse_url to parse a given URL into its individual components

All theses functions work fine but more than often you end up in situations when you have to add extra codes to completely deal with URLs. That’s when League\URL comes handy.

I’m happy to say that a huge work was done behind the scene to clean up the code since the move and the result is a completely new library easier to use. The new library now:

  • Treat FTPs URLs as well
  • Treat Urls as well define value objects.
  • Uses a common interface to access and manipule Url components classes to avoid any code lingo.
  • Introduce a \League\Url\UrlImmutable object which is the immutable version of the \League\Url\Url class.

A very basic usage of the new library is as fellow:

require 'vendor/autoload.php' //when using composer

use League\Url\Url;

$url = Url::createFromUrl('http://www.example.com');
$url->setQuery(['location' => 'San Francisco']);
echo $url; // http://www.example.com/path/to/index.php?location=San%20Francisco;
echo $url->getBaseUrl(); // http://www.example.com
echo $url->getRelativeUrl(); // /path/to/index.php?location=San%20Francisco;

You can already found the library on packagist and get the full documentation on github. Of course, you can always contribute by forking and making some pull request against the github repository

One thought on “Handling URLs with League\URL

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.