PHPackages                             ali-translator/url-template - 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. [Templating &amp; Views](/categories/templating)
4. /
5. ali-translator/url-template

ActiveLibrary[Templating &amp; Views](/categories/templating)

ali-translator/url-template
===========================

Helping on work with template url, example: "{country}.example.com/{language}/{city}"

v0.3.1(10mo ago)31.4kMITPHPPHP &gt;=7.4 &lt;8.5

Since Oct 16Pushed 10mo ago2 watchersCompare

[ Source](https://github.com/ali-translator/url-template)[ Packagist](https://packagist.org/packages/ali-translator/url-template)[ RSS](/packages/ali-translator-url-template/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (3)Versions (11)Used By (0)

URL Template
============

[](#url-template)

Helps work with URLs using their "base" template.
For example, the base structure of your project URL is *"gb.example.com/en/london/"*. In this example, your template URL has the following parameters: "country," "language," and "city."
Let's create a template for this example: "{country}.example.com/{language}/{city}"

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

[](#installation)

```
$ composer require ali-translator/url-template
```

Code example:
-------------

[](#code-example)

```
use ALI\UrlTemplate\UrlTemplateConfig;
use ALI\UrlTemplate\UrlTemplateResolver;

$urlTemplateConfig = new UrlTemplateConfig(
    '{country}.example.com',
    '{language}/{city}/',
    // Regular expressions matching the parameters
    [
        'country' => ['uk', 'ua', 'pl'],
        'language' => '[a-z]{2}', // be careful with some free regular expressions
        'city' => ['kiev', 'berlin', 'paris', 'london'],
    ],
    // If you have some default parameters that may be empty in url, set them here
    [
        'city' => 'berlin',
        'language' => 'en',
    ],
    // "Hide default URL parameters?".
    // Can be an array if you want to hide only some parameters.
    true
);
$urlTemplateResolver = new UrlTemplateResolver($urlTemplateConfig);

$url = 'https://gb.example.com/de/london/';

// Parse existing URL
$parsedUrlTemplate = $urlTemplateResolver->parseCompiledUrl($url);
var_dump($parsedUrlTemplate->getFullParameters());

// Change some parameter on existing URL
$parsedUrlTemplate->setParameter('country', 'pl');
$urlWithAnotherCountry = $urlTemplateResolver->compileUrl($parsedUrlTemplate);
var_dump($urlWithAnotherCountry);

// Get clear URL (without template parameters) for application routing
$simplifiedUrl = $urlTemplateResolver->getSimplifiedUrl($parsedUrlTemplate);
var_dump($simplifiedUrl); // -> "https://example.com"

// Generate full URL from simplified URL (which application returns)
$parsedUrlTemplate = $urlTemplateResolver->generateParsedUrlTemplate('https://example.com/some-category/item?sale=1', [
    'country' => 'uk',
    'city' => 'london',
     // 'language' => 'en', // Default values may be skipped
]);
$compiledUrl = $urlTemplateResolver->compileUrl($parsedUrlTemplate);
var_dump($compiledUrl); // -> "https://uk.example.com/london/some-category/item?sale=1"

// As you may see, the default language value "en" is omitted in the URL.
// If you want it included in the URL, you must set "false" for the last parameter "isHideDefaultParameters" in the constructor of `UrlTemplateConfig`.
```

**Warning**: Be careful with free-form regular expressions. For language parameters, '\[a-z\]{2}' is less safe than using an explicit list like '(en|de|ua)'.

#### Optional Default Values

[](#optional-default-values)

You can set optional default values for parameters by providing a callable argument as the default value.
**Your optional parameter must depend only on required arguments.**

Example of use:

```
use ALI\UrlTemplate\UrlTemplateConfig;

$urlTemplateConfig = new UrlTemplateConfig(
    '{country}.test.com',
    '/{language}/',
    [
        'country' => ['tr', 'gb'],
        'language' => ['en', 'tr', 'de'],
    ],
    [
        'language' => function ($requiredParameters) {
            $languagesByCountries = ['tr'=>'tr', 'gb'=>'en'];

            return $languagesByCountries[$requiredParameters['country']] ??
                throw new Exception('Invalid country alias');
        },
    ],
    true
);
```

### Parameter Decorators

[](#parameter-decorators)

Sometimes you need to apply decorations to your parameters in a URL.
For example, if you want the following path template to be "/{country}-{language}/" and decide to hide the default language.
In this case, without decorators, you would get the following compiled URL: "/country-/".
The excessive character "-" looks unappealing.
You can use decorators to solve this problem.
A decorator is a class that implements the `ParameterDecoratorInterface`.

Example of use:

```
use ALI\UrlTemplate\ParameterDecorators\WrapperParameterDecorator;
use ALI\UrlTemplate\UrlTemplateConfig;

$urlTemplateConfig = new UrlTemplateConfig(
    null,
    '/{country}{language}/',
    [
        'country' => ['ua', 'pl'],
        'language' => ['ua', 'en', 'de'],
    ],
    [
        'city' => 'berlin',
        'language' => 'en',
    ],
    true,
    [
        'language' => new WrapperParameterDecorator('-'),
    ]
);
```

**For the decorator to work correctly, use an array of requirements with available values, instead of a regular expression.**

### Validate ParsedTemplate Object

[](#validate-parsedtemplate-object)

```
use \ALI\UrlTemplate\UrlTemplateResolver\ParsedUrlTemplateValidator;
use \ALI\UrlTemplate\ParsedUrlTemplate;

/** @var ParsedUrlTemplate $parsedUrlTemplate */

$urlTemplateValidator = new ParsedUrlTemplateValidator();
$errors = $urlTemplateValidator->validateParameters($parsedUrlTemplate);
// $errors : [key -> (string)'error description']
```

### Additional Features

[](#additional-features)

- You can use templates where multiple parameters are placed in one "URL namespace," such as the host "{country}-{language}-{currency}.test.com" and the path "/{country}-{language}/".
    - If you need to compile only the "host URL" or "path URL," you can use:

    ```
    $urlTemplateResolver->compileUrl($parsedUrl, $urlTemplateResolver::COMPILE_TYPE_HOST);
    ```
- If you need to skip only some default parameters in the URL, you can pass an array of parameter names to the $hideDefaultParametersFromUrl parameter of the UrlTemplateConfig class.
- If you have an optional parameter that depends on another parameter, and this other parameter is in a different part of the URL (e.g., the optional parameter is in the "path URL part" and depends on a parameter in the "host URL part"), there may be an issue when processing a relative URL without the host.
     To handle this scenario, pass a value to the function that determines the optional parameter:
    ```
    ...
    [
      'language' => function ($requiredParametersValues) use ($currentCountryAlias) {
          $countryAlias = $requiredParametersValues['country'] ?? $currentCountryAlias;
    ...
    ```
- To create a new `UrlTemplateConfig` based on an existing one: ```
    /** @var $urlTemplateConfig ALI\UrlTemplate\UrlTemplateConfig */
    $urlTemplateConfigData = $urlTemplateConfig->generateUrlTemplateConfigData();
    // Modify some config data
    $urlTemplateConfigData->setDefaultUrlSchema('https');
    // Create a new UrlTemplateConfig
    $newUrlTemplateConfig = $urlTemplateConfigData->generateUrlTemplateConfig();
    ```
- By default, the system allows the use of all subdomains for a given domain template.
    For example, if we have a template `{city}.test.com`, it will correctly handle the domain `www.lviv.test.com`.
    However, if we need the system to restrict handling of subdomains, we can specify in `UrlTemplateConfig` that subdomains should not be supported: ```
    $urlTemplateConfig->setIsAllowedSubdomains(false);
    ```

### Tests

[](#tests)

Included in the package is a docker-compose file, with an environment for testing.

```
docker-compose up -d
docker-compose exec php bash
composer install
./vendor/bin/phpunit
./vendor/bin/phpstan analyse src tests
```

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance55

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity68

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

Recently: every ~384 days

Total

9

Last Release

300d ago

PHP version history (4 changes)v0.1PHP &gt;=5.6.0

v0.2PHP &gt;=7.0

v0.3.0PHP &gt;=7.4

v0.3.1PHP &gt;=7.4 &lt;8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/c6776f2e6f6424abbc3c008f2016e016c708ac297e6bb3dc06fbc25908fca7db?d=identicon)[Slayer](/maintainers/Slayer)

---

Top Contributors

[![Slayer911](https://avatars.githubusercontent.com/u/9704032?v=4)](https://github.com/Slayer911 "Slayer911 (46 commits)")

---

Tags

urltemplateurl templateALIsli

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ali-translator-url-template/health.svg)

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

###  Alternatives

[phpoffice/phpword

PHPWord - A pure PHP library for reading and writing word processing documents (OOXML, ODF, RTF, HTML, PDF)

7.6k34.7M186](/packages/phpoffice-phpword)[rize/uri-template

PHP URI Template (RFC 6570) supports both expansion &amp; extraction

420137.3M46](/packages/rize-uri-template)[mopa/bootstrap-sandbox-bundle

Seperate live docs from code

256.8k](/packages/mopa-bootstrap-sandbox-bundle)[larablocks/pigeon

A more flexible email message builder for Laravel 5 including chained methods, reusable message configurations, and message layout and template view management.

143.7k](/packages/larablocks-pigeon)

PHPackages © 2026

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