{"id":2249,"date":"2014-06-25T11:41:42","date_gmt":"2014-06-25T09:41:42","guid":{"rendered":"http:\/\/www.nyamsprod.com\/blog\/?p=2249"},"modified":"2015-06-18T11:43:08","modified_gmt":"2015-06-18T09:43:08","slug":"handling-urls-with-leagueurl","status":"publish","type":"post","link":"https:\/\/nyamsprod.com\/blog\/handling-urls-with-leagueurl\/","title":{"rendered":"Handling URLs with League\\URL"},"content":{"rendered":"<div class=\"message warning\">\n<p><strong>Attention:<\/strong> Les informations de ce billet sont susceptibles d'&ecirc;tre obsol&egrave;tes car vieux de plus 2 ans.<\/p>\n<p><strong>Warning: <\/strong> The information you are reading may be obsolete, this post was published more than 2 years ago.<\/p>\n<\/div><p>While moving my CSV library over to <a title=\"The League of Extraordinary Packages\" href=\"http:\/\/thephpleague.com\/\" target=\"_blank\">thephpleague<\/a> 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.<!--more--><\/p>\n<p>A basic PHP installation comes with 3 powerful functions:<\/p>\n<ul>\n<li><code>http_build_query<\/code> to transform an array into a query string<\/li>\n<li><code>parse_str<\/code> that does the opposite and given a query string transforms it into a PHP array<\/li>\n<li>And last but not least <code>parse_url<\/code> to parse a given URL into its individual components<\/li>\n<\/ul>\n<p>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&#8217;s when <code>League\\URL<\/code> comes handy.<\/p>\n<p>I&#8217;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:<\/p>\n<ul>\n<li>Treat FTPs URLs as well<\/li>\n<li>Treat Urls as well define value objects.<\/li>\n<li>Uses a common interface to access and manipule Url components classes to avoid <a title=\"A case against coding lingo\" href=\"https:\/\/medium.com\/@frankdejonge\/a-case-against-coding-lingo-8ffae1a4fa4e\" target=\"_blank\">any code lingo<\/a>.<\/li>\n<li>Introduce a <code>\\League\\Url\\UrlImmutable<\/code> object which is the immutable version of the <code>\\League\\Url\\Url<\/code> class.<\/li>\n<\/ul>\n<p>A very basic usage of the new library is as fellow:<\/p>\n<pre><code class=\"language-php\">require 'vendor\/autoload.php' \/\/when using composer\r\n\r\nuse League\\Url\\Url;\r\n\r\n$url = Url::createFromUrl('http:\/\/www.example.com');\r\n$url->setPath('\/path\/to\/index.php');\r\n$url->setQuery(['location' => 'San Francisco']);\r\necho $url; \/\/ http:\/\/www.example.com\/path\/to\/index.php?location=San%20Francisco;\r\necho $url->getBaseUrl(); \/\/ http:\/\/www.example.com\r\necho $url->getRelativeUrl(); \/\/ \/path\/to\/index.php?location=San%20Francisco;\r\n<\/code><\/pre>\n<p>You can already found the library on <a href=\"http:\/\/packagist.org\/theleague\/url\" title=\"League\\URL on packagist\" target=\"_blank\">packagist<\/a> and get the full documentation on <a href=\"http:\/\/url.thephpleague.com\/\" title=\"The Full Documentation\" target=\"_blank\">github<\/a>. Of course, you can always contribute by forking and making some pull request against the <a href=\"https:\/\/github.com\/thephpleague\/url\" title=\"League\/Url source code\">github repository<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>League\\URLs: manipulating URLs made easy in PHP.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[769,785,786],"class_list":["post-2249","post","type-post","status-publish","format-standard","hentry","category-web","tag-php-league","tag-thephpleague","tag-url"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/comments?post=2249"}],"version-history":[{"count":5,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2249\/revisions"}],"predecessor-version":[{"id":2272,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2249\/revisions\/2272"}],"wp:attachment":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/media?parent=2249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/categories?post=2249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/tags?post=2249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}