PHPackages                             tobento/service-uri - 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. tobento/service-uri

ActiveLibrary

tobento/service-uri
===================

The uri service provides useful classes to deal with URLs in your applications.

2.0(5mo ago)02432MITPHPPHP &gt;=8.4

Since Jul 7Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-uri)[ Packagist](https://packagist.org/packages/tobento/service-uri)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-uri/feed)WikiDiscussions 2.x Synced today

READMEChangelog (5)Dependencies (5)Versions (7)Used By (2)

Uri Service
===========

[](#uri-service)

The uri service provides useful classes to deal with URLs in your applications.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
- [Documentation](#documentation)
    - [Urls](#urls)
    - [Uri Path](#uri-path)
    - [Uri Query](#uri-query)
    - [Uri Request](#uri-request)
    - [Base Uri](#base-uri)
    - [Current Uri](#current-uri)
    - [Previous Uri](#previous-uri)
    - [Asset Uri](#asset-uri)
    - [Base Path Resolver](#base-path-resolver)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the uri service running this command.

```
composer require tobento/service-uri

```

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

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design
- Extendable

Documentation
=============

[](#documentation)

Urls
----

[](#urls)

A place to store your applications urls for later usage.

```
use Tobento\Service\Uri\Urls;
use Psr\Http\Message\UriInterface;
use Nyholm\Psr7\Factory\Psr17Factory;

$urls = new Urls(uriFactory: new Psr17Factory());

// set urls
$urls->set('home', 'https://www.example.com');
$urls->set('backend', 'https://www.example.com/backend/');

// get urls
var_dump($urls->get('home'));
// string(23) "https://www.example.com"

// get urls with a default url
var_dump($urls->get('home.de', 'https://www.example.com/de'));
// string(26) "https://www.example.com/de"

// get uri
var_dump($urls->getUri('home') instanceof UriInterface);
// bool(true)

// get uri with default url
$uri = $urls->getUri('home.de', 'https://www.example.com/de');

// build url
var_dump($urls->build('home', path: 'foo/bar'));
// string(31) "https://www.example.com/foo/bar"

// get all urls
foreach($urls->all() as $key => $url) {
    //
}
```

You might define a custom builder:

```
use Tobento\Service\Uri\Urls;
use Nyholm\Psr7\Factory\Psr17Factory;

$urls = new Urls(uriFactory: new Psr17Factory());

$urls->set(
    'home',
    'https://www.example.com',
    function(string $uri, ?string $path): string {
        // do custom building
        return $uri;
    }
);

// only on the build method your custom builder gets called.
var_dump($urls->build('home', path: 'foo/bar'));
// string(23) "https://www.example.com"
```

Uri Path
--------

[](#uri-path)

The UriPath class is immutable, all transform methods return a new instance.

```
use Tobento\Service\Uri\UriPath;

$path = new UriPath('/foo/bar');

// creating a new path
$newPath = $path->withPath('/foo/bar/new/');

// get the path string
$pathString = $path->get();
$pathString = (string)$path;

// Subtract a string from the beginning path
var_dump($path->sub('/foo')->get());
// string(4) "/bar"

// Decode path
var_dump($path->withPath('foo%20bar')->decode()->get());
// string(7) "foo bar"

// Encode path
var_dump($path->withPath('foo bar')->encode()->get());
// string(9) "foo%20bar"
```

Path segments:

```
use Tobento\Service\Uri\UriPath;

$path = new UriPath('/foo/bar');

// creating a new path with segments
var_dump($path->withSegments(['foo', 'bar'])->get());
// string(7) "foo/bar"

var_dump($path->withSegments(['', 'foo', 'bar'])->get());
// string(8) "/foo/bar"

// get all segments
foreach($path->getSegments() as $segment) {
    var_dump($segment);
    // string(0) ""
    // string(3) "foo"
    // string(3) "bar"
}

// get segment, starting from 1.
var_dump($path->getSegment(1));
// string(0) ""

var_dump($path->getSegment(2));
// string(3) "foo"

var_dump($path->getSegment(4, default: 'value'));
// string(5) "value"

// prepend a segment from the index specified
var_dump($path->prependSegment('prepended', index: 2)->get());
// string(18) "/prepended/foo/bar"

// append a segment from the index specified
var_dump($path->appendSegment('appended', index: 2)->get());
// string(17) "/foo/appended/bar"

// delete a segment from the index specified
var_dump($path->deleteSegment(index: 2)->get());
// string(4) "/bar"
```

Uri Query
---------

[](#uri-query)

The UriQuery class is immutable, all transform methods return a new instance.

```
use Tobento\Service\Uri\UriQuery;

// from string
$query = new UriQuery('arg=value&arg1=value1');

// from array
$query = new UriQuery(['arg' => 'value', 'arg1' => 'value1']);

// creating a new query
$newQuery = $query->withQuery('arg=value&arg1=new');

// get the query string
$queryString = $query->get();
$queryString = (string)$query;

// add a parameter
var_dump($query->add('name', 'value')->get());
// string(32) "arg=value&arg1=value1&name=value"

// delete a parameter
var_dump($query->delete('arg')->get());
// string(11) "arg1=value1"

// modify parameters
var_dump(
    $query->modify([
        'arg' => 'new',
        'foo' => '1',
    ])->get()
);
// string(25) "arg=new&arg1=value1&foo=1"

// decode query
var_dump($query->withQuery('arg=foo%20bar&arg1=value1&foo=1')->decode()->get());
// string(29) "arg=foo bar&arg1=value1&foo=1"

// encode query
var_dump($query->withQuery('arg=foo bar&arg1=value1&foo=1')->encode()->get());
// string(31) "arg=foo%20bar&arg1=value1&foo=1"
```

Uri Request
-----------

[](#uri-request)

The UriRequest class is immutable, all with methods return a new instance.

```
use Tobento\Service\Uri\UriRequest;
use Tobento\Service\Uri\UriPath;
use Tobento\Service\Uri\UriQuery;

$uri = new UriRequest('foo?arg=value');

// get the uri string
$uriString = $uri->get();
$uriString = (string)$uri;

// with a new path
var_dump($uri->withPath('bar')->get());
// string(13) "bar?arg=value"

var_dump($uri->withPath(new UriPath('bar'))->get());
// string(13) "bar?arg=value"

// with a new query
var_dump($uri->withQuery('arg=new')->get());
// string(11) "foo?arg=new"

var_dump($uri->withQuery(['arg' => 'new'])->get());
// string(11) "foo?arg=new"

var_dump($uri->withQuery(new UriQuery('arg=new'))->get());
// string(11) "foo?arg=new"

// get the path
var_dump($uri->path() instanceof UriPath);
// bool(true)

// check if uri has a query
var_dump($uri->hasQuery());
// bool(true)

// get the query, this returns null if no query exists.
var_dump($uri->query() instanceof UriQuery);
// bool(true)
```

Base Uri
--------

[](#base-uri)

The BaseUri class might be useful for your application.

```
use Tobento\Service\Uri\BaseUri;
use Tobento\Service\Uri\BaseUriInterface;
use Psr\Http\Message\UriInterface;
use Nyholm\Psr7\Factory\Psr17Factory;

$uri = new Psr17Factory()->createUri('https://example.com/base/path/');

$baseUri = new BaseUri($uri);

var_dump($baseUri instanceof UriInterface);
// bool(true)

var_dump($baseUri instanceof BaseUriInterface);
// bool(true)
```

Current Uri
-----------

[](#current-uri)

The CurrentUri class might be useful for your application.

```
use Tobento\Service\Uri\CurrentUri;
use Tobento\Service\Uri\CurrentUriInterface;
use Psr\Http\Message\UriInterface;
use Nyholm\Psr7\Factory\Psr17Factory;

$uri = new Psr17Factory()->createUri('https://example.com/current/path/');

$currentUri = new CurrentUri($uri, isHome: true);

var_dump($currentUri instanceof UriInterface);
// bool(true)

var_dump($currentUri instanceof CurrentUriInterface);
// bool(true)

var_dump($currentUri->isHome());
// bool(true)
```

Previous Uri
------------

[](#previous-uri)

The PreviousUri class might be useful for your application.

```
use Tobento\Service\Uri\PreviousUri;
use Tobento\Service\Uri\PreviousUriInterface;
use Psr\Http\Message\UriInterface;
use Nyholm\Psr7\Factory\Psr17Factory;

$uri = new Psr17Factory()->createUri('https://example.com/previous/path/');

$previousUri = new PreviousUri($uri);

var_dump($previousUri instanceof UriInterface);
// bool(true)

var_dump($previousUri instanceof PreviousUriInterface);
// bool(true)
```

Asset Uri
---------

[](#asset-uri)

The AssetUri class might be useful for your application.

```
use Tobento\Service\Uri\AssetUri;
use Tobento\Service\Uri\AssetUriInterface;
use Psr\Http\Message\UriInterface;
use Nyholm\Psr7\Factory\Psr17Factory;

$uri = new Psr17Factory()->createUri('https://example.com/asset/path/');

$assetUri = new AssetUri($uri);

var_dump($assetUri instanceof UriInterface);
// bool(true)

var_dump($assetUri instanceof BaseUriInterface);
// bool(true)
```

Base Path Resolver
------------------

[](#base-path-resolver)

```
use Tobento\Service\Uri\BasePathResolver;
use Tobento\Service\Uri\BasePathResolverInterface;
use Nyholm\Psr7\Factory\Psr17Factory;

$serverRequest = new Psr17Factory()->createServerRequest(
    'GET',
    'https://example.com',
    ['SCRIPT_NAME' => '/foo/uri.php']
);

var_dump((new BasePathResolver($serverRequest))->resolve());
// string(4) "/foo"

var_dump(new BasePathResolver($serverRequest) instanceof BasePathResolverInterface);
// bool(true)
```

Credits
=======

[](#credits)

- [Tobias Strub](https://www.tobento.ch)
- [All Contributors](../../contributors)

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance70

Regular maintenance activity

Popularity14

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity72

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 ~257 days

Recently: every ~211 days

Total

7

Last Release

172d ago

Major Versions

1.x-dev → 2.02025-10-01

PHP version history (2 changes)1.0.0PHP &gt;=8.0

2.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/055d6a1b5c2384bb179c75ab0b55914231d898fdc4dffeb30770f81200e52206?d=identicon)[TOBENTOch](/maintainers/TOBENTOch)

---

Top Contributors

[![tobento-ch](https://avatars.githubusercontent.com/u/16684832?v=4)](https://github.com/tobento-ch "tobento-ch (10 commits)")

---

Tags

uripackagepathtobento

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tobento-service-uri/health.svg)

```
[![Health](https://phpackages.com/badges/tobento-service-uri/health.svg)](https://phpackages.com/packages/tobento-service-uri)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k997.0M2.7k](/packages/guzzlehttp-psr7)[league/uri-components

URI components manipulation library

31830.1M52](/packages/league-uri-components)[josantonius/url

PHP library to access URL information.

123.2k2](/packages/josantonius-url)

PHPackages © 2026

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