PHPackages                             thinktomorrow/url - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. thinktomorrow/url

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

thinktomorrow/url
=================

A simple php url utility class.

4.0.0(1y ago)140.8k↑11.8%[1 PRs](https://github.com/thinktomorrow/url/pulls)2MITPHPPHP &gt;=8.2CI passing

Since May 21Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/thinktomorrow/url)[ Packagist](https://packagist.org/packages/thinktomorrow/url)[ Docs](https://github.com/thinktomorrow/url)[ RSS](/packages/thinktomorrow-url/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (9)Used By (2)

Url
===

[](#url)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c53c3acb21e7bde08b9ace8320d72ae696c877cd1a709680b70ca61fa0cadbfe/68747470733a2f2f706f7365722e707567782e6f72672f7468696e6b746f6d6f72726f772f75726c2f762f737461626c653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/thinktomorrow/url)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Test](https://github.com/thinktomorrow/url/workflows/Test/badge.svg)](https://github.com/thinktomorrow/url/workflows/Test/badge.svg)[![Quality Score](https://camo.githubusercontent.com/24cd5d1556ba200bbf555a383e04e80eaaae6c80c2f25626f940b487121d9e75/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7468696e6b746f6d6f72726f772f75726c2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/thinktomorrow/url)[![Total Downloads](https://camo.githubusercontent.com/914e61492ac304e1971a0891f4ce350d823ed99dc42b288635f46d40f6484b14/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7468696e6b746f6d6f72726f772f75726c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/thinktomorrow/url)

A URL helper class to easily extract certain parts of the url. It is basically a wrapper around the native `parse_url` function. Currently, `parse_url` has no solid support for parsing uri strings. This package aims to provide that support.

This small package is framework agnostic and has no dependencies.

Install via composer
--------------------

[](#install-via-composer)

```
$ composer require thinktomorrow/url
```

Usage
-----

[](#usage)

### Create an Url instance

[](#create-an-url-instance)

Create a new url instance by calling the static `fromString` method with your url string.

```
\Thinktomorrow\Url\Url::fromString('https://example.com');
```

In case of a malformed url string, an `InvalidUrl` exception will be thrown. This validation is based on what the native `parse_url` considers a malformed url string.

### Parts of the url

[](#parts-of-the-url)

Via the Url instance, you have access to all the different parts of the url string. You can retrieve the following parts:

```
// scheme
\Thinktomorrow\Url\Url::fromString('https://example.com')->getScheme(); // https

// host
\Thinktomorrow\Url\Url::fromString('https://example.com')->getHost(); // example.com

// port
\Thinktomorrow\Url\Url::fromString('https://example.com:9000')->getPort(); // 9000

// path
\Thinktomorrow\Url\Url::fromString('https://example.com/foo/bar')->getPath(); // foo/bar

// query
\Thinktomorrow\Url\Url::fromString('https://example.com?foo=bar')->getQuery(); // foo=bar

// hash
\Thinktomorrow\Url\Url::fromString('https://example.com#foobar')->getHash(); // foobar
```

Altering the url string
-----------------------

[](#altering-the-url-string)

### Securing a url

[](#securing-a-url)

You can secure an url with the `secure()` method.

```
Url::fromString('example.com')->secure()->get(); // 'https://example.com'
```

You can force a non-secure scheme with the `nonSecure` method.

```
Url::fromString('example.com')->nonSecure()->get(); // 'http://example.com'
Url::fromString('https://example.com')->nonSecure()->get(); // 'http://example.com'
```

### Changing url root

[](#changing-url-root)

In the case you need to change the url root, you can use the `setCustomRoot` method. This expects a `\Thinktomorrow\Url\Root` object as argument.

```
Url::fromString('http://example.com/foobar')
    ->setCustomRoot(Root::fromString('https://newroot.be'))
    ->get(); // 'https://newroot.be/foobar'
```

### Localizing the url

[](#localizing-the-url)

In case you use the url path segment for localization purposes, you can inject the locale segment with the `localize` method

```
Url::fromString('http://example.com/foobar')
    ->localize('en')
    ->get(); // 'http://example.com/en/foobar'
```

The `localize` method also accepts a second parameter to enlist all available locales. In the case that passed url contains any of these locales, the existing locale will be replaced by the new locale.

```
Url::fromString('http://example.com/en/foobar')
    ->localize('fr', ['en','fr'])
    ->get(); // 'http://example.com/fr/foobar'
```

If you pass `null` as the locale parameter, any locale segment will be removed.

```
Url::fromString('http://example.com/en/foobar')
    ->localize(null, ['en','fr'])
    ->get(); // 'http://example.com/foobar'
```

Testing
-------

[](#testing)

```
$ vendor/bin/phpunit
```

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- Ben Cavens
- Philippe Damen

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance62

Regular maintenance activity

Popularity30

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~343 days

Recently: every ~321 days

Total

7

Last Release

496d ago

Major Versions

1.0.3 → 2.0.02021-12-14

2.0.0 → 3.0.02024-04-20

3.0.0 → 4.0.02025-01-07

PHP version history (4 changes)1.0.0PHP &gt;=7.1.3

1.0.3PHP &gt;=7.4

2.0.0PHP &gt;=8.1

4.0.0PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/16da702ae46c18e3c7dc28ef2110662bbb0476134fa0d8972b3fce2c170e83cf?d=identicon)[yinx](/maintainers/yinx)

---

Top Contributors

[![BenCavens](https://avatars.githubusercontent.com/u/497668?v=4)](https://github.com/BenCavens "BenCavens (59 commits)")

---

Tags

urlthinktomorrow

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/thinktomorrow-url/health.svg)

```
[![Health](https://phpackages.com/badges/thinktomorrow-url/health.svg)](https://phpackages.com/packages/thinktomorrow-url)
```

###  Alternatives

[spatie/url

Parse, build and manipulate URL's

73914.3M97](/packages/spatie-url)[jbroadway/urlify

A fast PHP slug generator and transliteration library that converts non-ascii characters for use in URLs.

6737.4M62](/packages/jbroadway-urlify)[league/uri-components

URI components manipulation library

31932.3M67](/packages/league-uri-components)[sabre/uri

Functions for making sense out of URIs.

29335.2M40](/packages/sabre-uri)[spomky-labs/base64url

Base 64 URL Safe Encoding/Decoding PHP Library

15439.5M49](/packages/spomky-labs-base64url)[misd/linkify

Converts URLs and email addresses in text into HTML links

1122.9M10](/packages/misd-linkify)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
