PHPackages                             model/router - 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. [Framework](/categories/framework)
4. /
5. model/router

ActiveLibrary[Framework](/categories/framework)

model/router
============

Routing module for ModEl Framework

v0.2.36(2mo ago)0359PHP

Since Oct 23Pushed 2mo agoCompare

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

READMEChangelogDependencies (8)Versions (57)Used By (0)

ModEl Router
============

[](#model-router)

A standalone, framework-agnostic PHP router with bidirectional routing support, multi-field URL segments, and relationship resolution.

Features
--------

[](#features)

- **Modern route syntax** with `:parameter` notation
- **Multi-field segments**: `/users/:name-:surname`
- **Relationship fields**: `/products/:category.name/:id-:name`
- **Bidirectional routing**: Parse URLs and generate URLs
- **Database integration** via dependency injection
- **Framework-agnostic**: Works as a standalone package

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

[](#installation)

The router is designed as a standalone package. You can use install it via Composer:

```
composer require model/router
```

Basic Usage
-----------

[](#basic-usage)

### 1. Create Router providers

[](#1-create-router-providers)

```
use \Model\Router\AbstractRouterProvider;

class RouterProvider extends AbstractRouterProvider {
    public function getRoutes(): void {
        return [
            [
                'pattern' => '/pages/:name',
                'controller' => 'PageController',
                'options' => [
                    'entity' => [
                        'table' => 'pages',
                    ],
                ],
            ],
            [
                'pattern' => '/users/:name-:surname',
                'controller' => 'UserController',
                'options' => [
                    'entity' => [
                        'table' => 'users',
                    ],
                ],
            ],
        ];
    }
}

### 2. Create Router

```php
use Model\Router\Router;

// If your route do not use database lookups
$router = new Router();

// If they use database lookups, provide a Resolver instance
$resolver = new YourDatabaseResolver(); // Implement the Resolver interface
$router = new Router($resolver);
```

### 3. Match Incoming URLs

[](#3-match-incoming-urls)

```
$url = '/pages/about-us';
$result = $router->match($url);

if ($result) {
	$controller = $result['controller']; // 'PageController'
	$params = $result['params']; // ['id' => 5] (from database lookup)
}
```

### 4. Generate URLs

[](#4-generate-urls)

```
// Generate URL with ID
$url = $router->generate('PageController', 5);
// Result: /pages/about-us

// Generate URL with explicit parameters
$url = $router->generate('UserController', [
	'name' => 'John',
	'surname' => 'Doe',
]);
// Result: /users/john-doe
```

Route Syntax
------------

[](#route-syntax)

### Simple Parameters

[](#simple-parameters)

```
[
    'pattern' => '/pages/:name',
    'controller' => 'PageController',
    'options' => [
        'entity' => [
            'table' => 'pages',
        ],
    ],
]
```

URL: `/pages/about-us` → Looks up page with `name = 'about-us'`

### Numeric IDs

[](#numeric-ids)

```
[
    'pattern' => '/pages/:id',
    'controller' => 'PageController',
]
```

URL: `/pages/123` → Directly matches ID 123

### Multiple Fields in One Segment

[](#multiple-fields-in-one-segment)

```
[
    'pattern' => '/pages/:name-:surname',
    'controller' => 'UserController',
    'options' => [
        'entity' => [
            'table' => 'users',
        ],
    ],
]
```

URL: `/users/john-doe-smith` → Tries combinations:

- `name='john'` AND `surname='doe-smith'`
- `name='john-doe'` AND `surname='smith'`

### Relationship Fields

[](#relationship-fields)

```
[
    'pattern' => '/products/:category.name/:id-:name',
    'controller' => 'ProductController',
    'options' => [
        'entity' => [
            'table' => 'products',
        ],
    ],
]
```

URL: `/products/electronics/123-laptop` → Looks up:

1. Category with `name = 'electronics'`
2. Product with `id = 123` AND `name = 'laptop'`

Route Options
-------------

[](#route-options)

### Available Options

[](#available-options)

- `table` (string): Database table for lookups
- `primary` (string): Primary key field name (default: 'id')
- `relationships` (array): Relationship configuration
- `case_sensitive` (bool): Case-sensitive matching (default: false)
- `tags` (array): Additional metadata for route filtering
- `lowercase` (bool): Convert generated URLs to lowercase (default: true)

### Example with All Options

[](#example-with-all-options)

```
[
    'pattern' => '/blog/:category.name/:id-:slug',
    'controller' => 'BlogController',
    'options' => [
        'entity' => [
            'table' => 'blog_posts',
        ],
        'case_sensitive' => false,
        'tags' => [
            'lang' => 'en',
            'type' => 'public',
        ],
        'lowercase' => true,
]
```

URL Generation with Tags
------------------------

[](#url-generation-with-tags)

Generate URLs for specific route variants using tags:

```
// Generate for specific language
$url = $router->generate('PageController', 5, ['lang' => 'en']);
// Result: /pages/about-us

$url = $router->generate('PageController', 5, ['lang' => 'it']);
// Result: /pagine/chi-siamo
```

Advanced Features
-----------------

[](#advanced-features)

### Custom URL Encoding

[](#custom-url-encoding)

The router automatically converts field values to URL-friendly format:

- Converts to lowercase
- Replaces spaces with dashes
- Removes special characters
- Supports Unicode characters (Cyrillic, Chinese, etc.)

### Caching

[](#caching)

The UrlGenerator caches database lookups during URL generation to minimize queries. Routes are also cached after first loading.

### Combination Algorithm

[](#combination-algorithm)

For multi-field segments, the router generates all possible word distributions and tries each until finding a match.

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance87

Actively maintained with recent releases

Popularity16

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

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

Recently: every ~10 days

Total

56

Last Release

62d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/44db30545ec9490b20b25e79a111a1f21e4e8363a0d58c5ac61bcdfe64e2e947?d=identicon)[Zorinik](/maintainers/Zorinik)

---

Top Contributors

[![Zorinik](https://avatars.githubusercontent.com/u/17624354?v=4)](https://github.com/Zorinik "Zorinik (69 commits)")

### Embed Badge

![Health badge](/badges/model-router/health.svg)

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

###  Alternatives

[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

712181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)[laravel/pail

Easily delve into your Laravel application's log files directly from the command line.

91545.3M590](/packages/laravel-pail)[nette/bootstrap

🅱 Nette Bootstrap: the simple way to configure and bootstrap your Nette application.

68535.8M592](/packages/nette-bootstrap)

PHPackages © 2026

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