PHPackages                             zero-to-prod/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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. zero-to-prod/url

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

zero-to-prod/url
================

A DataModel for a Url

v82.1.0(10mo ago)11.3k2MITPHPPHP &gt;=7.1CI passing

Since Sep 3Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/zero-to-prod/url)[ Packagist](https://packagist.org/packages/zero-to-prod/url)[ Docs](https://github.com/zero-to-prod/url)[ Fund](https://github.com/sponsors/zero-to-prod)[ RSS](/packages/zero-to-prod-url/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (4)Versions (19)Used By (2)

Zerotoprod\\Url
===============

[](#zerotoprodurl)

[![](./art/logo.png)](./art/logo.png)

[![Repo](https://camo.githubusercontent.com/9a90a3efeee26aed7d7f2feee9cd84566a26f9c362cc773b184d076210906e1c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6769746875622d677261793f6c6f676f3d676974687562)](https://github.com/zero-to-prod/url)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/92e57328ada54c85eba73bd5b141e1b158cb58e12262fa458d06845555986e6f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7a65726f2d746f2d70726f642f75726c2f746573742e796d6c3f6c6162656c3d74657374)](https://github.com/zero-to-prod/url/actions)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/714630e419e18c9f96c7306fce2f9601de4574fd9ed5937501b2e978fd05585b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7a65726f2d746f2d70726f642f75726c2f6261636b77617264735f636f6d7061746962696c6974792e796d6c3f6c6162656c3d6261636b77617264735f636f6d7061746962696c697479)](https://github.com/zero-to-prod/url/actions)[![Packagist Downloads](https://camo.githubusercontent.com/5adf1cc8b77506ad650cf484a01f60c3cf96556f8dd17ee3dfdc8b65155ea41a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7a65726f2d746f2d70726f642f75726c3f636f6c6f723d626c7565)](https://packagist.org/packages/zero-to-prod/url/stats)[![Packagist Version](https://camo.githubusercontent.com/c66f2ccea95ba969bb8a325165f7d441dd81cb14a1e406f771f3258b67e414bb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a65726f2d746f2d70726f642f75726c3f636f6c6f723d663238643161)](https://packagist.org/packages/zero-to-prod/url)[![GitHub repo size](https://camo.githubusercontent.com/d5023131114fae803053fd5df767827716b3708aa6052c2e3bf34b569d83a4bc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7265706f2d73697a652f7a65726f2d746f2d70726f642f75726c)](https://github.com/zero-to-prod/url)[![License](https://camo.githubusercontent.com/c2833553925626d8ff8207c9dcfc4db106531e3026f5fb6732d7d68fb303db85/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7a65726f2d746f2d70726f642f75726c3f636f6c6f723d726564)](https://github.com/zero-to-prod/url/blob/main/LICENSE.md)[![Hits-of-Code](https://camo.githubusercontent.com/821543cbde18aa96c0be54c5d890fe38f9e4649d36c61e3e9d958b05880d9f0b/68747470733a2f2f686974736f66636f64652e636f6d2f6769746875622f7a65726f2d746f2d70726f642f75726c3f6272616e63683d6d6173746572)](https://hitsofcode.com/github/zero-to-prod/url/view?branch=master)

Contents
--------

[](#contents)

- [Introduction](#introduction)
- [Requirements](#requirements)
- [Installation](#installation)
- [Documentation Publishing](#documentation-publishing)
    - [Automatic Documentation Publishing](#automatic-documentation-publishing)
- [Usage](#usage)
    - [Helper Methods](#helper-methods)
- [Suggested Traits](#suggested-traits)
- [Local Development](./LOCAL_DEVELOPMENT.md)
- [Contributing](#contributing)

Introduction
------------

[](#introduction)

Parse and access url components with a class.

Requirements
------------

[](#requirements)

- PHP 7.1 or higher.

Installation
------------

[](#installation)

Install `Zerotoprod\Url` via [Composer](https://getcomposer.org/):

```
composer require zero-to-prod/url
```

This will add the package to your project’s dependencies and create an autoloader entry for it.

Documentation Publishing
------------------------

[](#documentation-publishing)

You can publish this README to your local documentation directory.

This can be useful for providing documentation for AI agents.

This can be done using the included script:

```
# Publish to default location (./docs/zero-to-prod/url)
vendor/bin/zero-to-prod-url

# Publish to custom directory
vendor/bin/zero-to-prod-url /path/to/your/docs
```

### Automatic Documentation Publishing

[](#automatic-documentation-publishing)

You can automatically publish documentation by adding the following to your `composer.json`:

```
{
  "scripts": {
    "post-install-cmd": [
      "zero-to-prod-url"
    ],
    "post-update-cmd": [
      "zero-to-prod-url"
    ]
  }
}
```

Usage
-----

[](#usage)

Use the `from()` static method to map array keys to class properties.

It is recommended to extend your own Url class with the `\Zerotoprod\Url\Url` class.

```
class Url extends \Zerotoprod\Url\Url
{
}

$url = Url::from(
    parse_url('example.com')
);

$url->host // 'example.com'

// Passing an array
$url = Url::new()
        ->set_host('example.com')
        ->set_scheme('https')
        ->set_path('/search')
        ->set_query('q=openai');

$url->host; // 'example.com'
```

### Helper Methods

[](#helper-methods)

The `Url` class includes several helper methods for constructing URLs with specific schemes and ports.

```
