PHPackages                             bakame/twig-domain-parser-extension - 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. bakame/twig-domain-parser-extension

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

bakame/twig-domain-parser-extension
===================================

Twig extension using PHP Domain parser.

111PHP

Since Dec 6Pushed 7y ago1 watchersCompare

[ Source](https://github.com/bakame-php/twig-domain-parser-extension)[ Packagist](https://packagist.org/packages/bakame/twig-domain-parser-extension)[ RSS](/packages/bakame-twig-domain-parser-extension/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (1)

Twig Domain parser extension
============================

[](#twig-domain-parser-extension)

[![Build Status](https://camo.githubusercontent.com/abc2a6de937ca6c1637632f33921b50be8267aaf7b8cbbf5931f46776a0aac61/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f62616b616d652d7068702f747769672d646f6d61696e2d7061727365722d657874656e73696f6e2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/bakame-php/twig-domain-parser-extension)[![Total Downloads](https://camo.githubusercontent.com/1c0a6b3456858a9e370ba4ea0663f7ee8ef154e1155a5a0463c05cf61a8d77e7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62616b616d652f747769672d646f6d61696e2d7061727365722d657874656e73696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bakame/twig-domain-parser-extension)[![Latest Stable Version](https://camo.githubusercontent.com/8d3c91c84f4496cff3d16a984bf76e71d75f311c2fe039a79bf20d5ab5c34148/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f62616b616d652d7068702f747769672d646f6d61696e2d7061727365722d657874656e73696f6e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/bakame-php/twig-domain-parser-extension/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/bakame-php/twig-domain-parser-extension/blob/master/LICENSE)

This package provides Twig extensions for PHP Domain parser v5.4+

System Requirements
-------------------

[](#system-requirements)

You need:

- **PHP &gt;= 7.1.0** but the latest stable version of PHP is recommended

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

[](#installation)

```
$ composer require bakame/twig-domain-parser-extension
```

Setup
-----

[](#setup)

```
use Bakame\Twig\Pdp\Extension;
use Pdp\Cache;
use Pdp\CurlHttpClient;
use Pdp\Manager;

$manager = new Manager(new Cache(), new CurlHttpClient(), '1 DAY');
$rules = $manager->getRules();
$topLevelDomains = $manager->getTLDs();

$twig->addExtension(new Extension($rules, $topLevelDomains));
```

Because the `Pdp\Cache` class implements PSR-16, you can use any PSR-16 compatible cache driver. For instance you can use the Symfony cache component instead:

```
$ composer require symfony/cache
```

```
use Bakame\Twig\Pdp\Extension;
use Pdp\CurlHttpClient;
use Pdp\Manager;
use Symfony\Component\Cache\Simple\PDOCache;

$dbh = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'dbuser', 'dbpass');
$cache = new PDOCache($dbh, 'psl', 86400);
$manager = new Manager($cache, new CurlHttpClient(), 86400);

$twig->addExtension(Extension::createFromManager($manager));
```

*You can directly get an `Extension` instance from a `Pdp\Manager` object using the `createFromManager` named constructor.*

Usage
-----

[](#usage)

### Manipulating a Domain object in Twig template

[](#manipulating-a-domain-object-in-twig-template)

```
{% set host = 'www.食狮.公司.cn' %}
hostname: {{ resolve_domain(host) }} {#  www.食狮.公司.cn #}
subDomain : {{ resolve_domain(host).subDomain }} {#  www #}
registrableDomain : {{ resolve_domain(host).registrableDomain }} {#  食狮.公司.cn #}
publicSuffix : {{ resolve_domain(host).publicSuffix }} {#  公司.cn #}
isICANN : {{ resolve_domain(host).ICANN ? 'ok' : 'ko' }} {#  ok #}
isPrivate : {{ resolve_domain(host).private ? 'ok' : 'ko' }} {# ko #}
isKnown : {{ resolve_domain(host).known ? 'ok' : 'ko' }} {#  ok #}
ascii : {{ resolve_domain(host).toAscii }} {#  www.xn--85x722f.xn--55qx5d.cn #}
unicode : {{ resolve_domain(host).toUnicode }} {#  www.食狮.公司.cn #}
label : {{ resolve_domain(host).label(0) }} {# cn #}
publicSuffix : {{ resolve_domain('foo.github.io', constant('Pdp\\Rules::PRIVATE_DOMAINS')).publicSuffix }} {# github.io #}
```

The `resolve_domain` function returns a `Pdp\Domain` object you can use to manipulate to returns various informations about your hostname. The returned object is resolved againts the PSL resources using `Pdp\Rules::resolve` method. This means that you can optionnally decide which section the domain should be resolve too.

The `resolve_domain` parameters are:

- `$host` a scalar or a stringable object
- `$section` : a string representing one of the PSL section
    - `Rules::ICANN_DOMAINS` : to resolve the domain against the PSL ICANN section
    - `Rules::PRIVATE_DOMAINS` : to resolve the domain against the PSL private section

By default the resolution is made against the section with the longest public suffix.

### Detecting if the host contains a IANA top level domain

[](#detecting-if-the-host-contains-a-iana-top-level-domain)

```
hostname: {{ host is topLevelDomain ? 'ok' : 'ko' }} {# ok #}
```

The `topLevelDomain` tests tells whether the submitted domain contains a known IANA top level domain

Contributing
------------

[](#contributing)

Contributions are welcome and will be fully credited. Please see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.

Testing
-------

[](#testing)

The library has a has a :

- a [PHPUnit](https://phpunit.de) test suite
- a coding style compliance test suite using [PHP CS Fixer](http://cs.sensiolabs.org/).
- a code analysis compliance test suite using [PHPStan](https://github.com/phpstan/phpstan).

To run the tests, run the following command from the project folder.

```
$ composer test
```

Security
--------

[](#security)

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

Credits
-------

[](#credits)

- [ignace nyamagana butera](https://github.com/nyamsprod)
- [All Contributors](https://github.com/thephpleague/uri-query-parser/contributors)

License
-------

[](#license)

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

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/51073?v=4)[Ignace Nyamagana Butera](/maintainers/nyamsprod)[@nyamsprod](https://github.com/nyamsprod)

---

Tags

domain-parserextensionicannphppublicsuffixlisttwig

### Embed Badge

![Health badge](/badges/bakame-twig-domain-parser-extension/health.svg)

```
[![Health](https://phpackages.com/badges/bakame-twig-domain-parser-extension/health.svg)](https://phpackages.com/packages/bakame-twig-domain-parser-extension)
```

###  Alternatives

[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[whitecube/nova-flexible-content

Flexible Content &amp; Repeater Fields for Laravel Nova.

8053.0M25](/packages/whitecube-nova-flexible-content)[mopa/bootstrap-bundle

Easy integration of twitters bootstrap into symfony2

7042.9M33](/packages/mopa-bootstrap-bundle)[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3871.2M](/packages/limenius-react-bundle)[nicmart/string-template

StringTemplate is a very simple string template engine for php. I've written it to have a thing like sprintf, but with named and nested substutions.

2101.7M30](/packages/nicmart-string-template)

PHPackages © 2026

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