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_queryto transform an array into a query string
parse_strthat does the opposite and given a query string transforms it into a PHP array
- And last but not least
parse_urlto 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\UrlImmutableobject which is the immutable version of the
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->setPath('/path/to/index.php'); $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;