### Install the package via Composer Source: https://github.com/spatie/url/blob/main/README.md Use this command to add the package to your project dependencies. ```bash composer require spatie/url ``` -------------------------------- ### Run package tests Source: https://github.com/spatie/url/blob/main/README.md Executes the test suite for the package using Composer. ```bash composer test ``` -------------------------------- ### Parse and retrieve URL components Source: https://github.com/spatie/url/blob/main/README.md Initialize a URL object from a string and access its individual components. ```php use Spatie\Url\Url; $url = Url::fromString('https://spatie.be/opensource'); echo $url->getScheme(); // 'https' echo $url->getHost(); // 'spatie.be' echo $url->getPath(); // '/opensource' ``` -------------------------------- ### Implement PSR-7 UriInterface Source: https://github.com/spatie/url/blob/main/README.md The Url class implements the standard PSR-7 interface. ```php class Url implements UriInterface { /* ... */ } ``` -------------------------------- ### Use allowed schemes Source: https://github.com/spatie/url/blob/main/README.md Restrict or validate schemes using a list of allowed values. ```php $url = Url::fromString('https://spatie.be/opensource'); echo $url->withAllowedSchemes(['wss'])->withScheme('wss'); // 'wss://spatie.be/opensource' ``` ```php $url = Url::fromString('https://spatie.be/opensource', [...SchemeValidator::VALID_SCHEMES, 'wss']); echo $url->withScheme('wss'); // 'wss://spatie.be/opensource' ``` -------------------------------- ### Manage query parameters Source: https://github.com/spatie/url/blob/main/README.md Retrieve, add, or remove query parameters from a URL. ```php $url = Url::fromString('https://spatie.be/opensource?utm_source=github&utm_campaign=packages'); echo $url->getQuery(); // 'utm_source=github&utm_campaign=packages' echo $url->getQueryParameter('utm_source'); // 'github' echo $url->getQueryParameter('utm_medium'); // null echo $url->getQueryParameter('utm_medium', 'social'); // 'social' echo $url->getQueryParameter('utm_medium', function() { //some logic return 'email'; }); // 'email' echo $url->withoutQueryParameter('utm_campaign'); // 'https://spatie.be/opensource?utm_source=github' echo $url->withQueryParameters(['utm_campaign' => 'packages']); // 'https://spatie.be/opensource?utm_source=github&utm_campaign=packages' ``` -------------------------------- ### Retrieve path segments Source: https://github.com/spatie/url/blob/main/README.md Access specific segments of the URL path by index. ```php $url = Url::fromString('https://spatie.be/opensource/laravel'); echo $url->getSegment(1); // 'opensource' echo $url->getSegment(2); // 'laravel' ``` -------------------------------- ### Transform URL scheme Source: https://github.com/spatie/url/blob/main/README.md Update the scheme of a URL instance. ```php $url = Url::fromString('http://spatie.be/opensource'); echo $url->withScheme('https'); // 'https://spatie.be/opensource' ``` -------------------------------- ### Transform URL components Source: https://github.com/spatie/url/blob/main/README.md Modify URL parts using the immutable Url class methods. ```php $url = Url::fromString('https://spatie.be/opensource'); echo $url->withHost('github.com')->withPath('spatie'); // 'https://github.com/spatie' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.