PHPackages                             sunnyflail/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. sunnyflail/router

ActiveLibrary

sunnyflail/router
=================

A simple routing utility

1.4.0(4y ago)069MITPHP

Since May 4Pushed 4y ago1 watchersCompare

[ Source](https://github.com/SunnyFlail/Router)[ Packagist](https://packagist.org/packages/sunnyflail/router)[ RSS](/packages/sunnyflail-router/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (7)Used By (0)

Router
======

[](#router)

A simple routing Utility

**How to use**
==============

[](#how-to-use)

This library leaves it up to you how you want to handle the resulting callback, focusing only on routing

1. **Adding new Routes**
------------------------

[](#1-adding-new-routes)

This library provides three ways to add new Routes to Router. **Starting with the most recommended way at 1.2**

### **1.1. Route creation schema**

[](#11-route-creation-schema)

Schema is almost the same for each of these methods

1. ***path*** - String with path that the router will match. It **MUST** end with a forwards backslash to work correctly. For segments with variable arguments contain the name of argument with curly brackets (eg. *`'/offer/{id}'`*)
2. ***callback*** - Array containing name of the class / object instance that the Route will be refering to (eg. *`[IndexController::class, "index"]`*). It may also be an anonymous function (eg. *`fn() => print("Hello from anonymous function!")`*)
3. (optional) ***methods*** - Array containg **uppercased** names of http methods that this route will correspond to (eg. *`["POST", "OPTION"]`*). Defaults to `["GET", "HEAD"]`
4. (optional) ***params*** - Associative array with argument names as keys and regexes that they match as values. If you used an argumented segment in path, you must provide a regex for it there (eg. *`["id" => "\d+"]`*)
5. (optional) ***defaults*** - Associative array with argument names as keys and defaulting values as value. You may provide them for some of argumented segments (eg.*`["id" => 1]`*)

### **1.2. Loading from an associative array**

[](#12-loading-from-an-associative-array)

Create new instance of Router and then call that instance's *`Router::insertConfig`* method, passing an array with params as shown above eg.

```
    use SunnyFlail\Router\Router;
    $router = new Router();
    $router->insertConfig([
        [
            "index",
            "/",
            fn() => print("Talking to you from '/'!"),
        ]
    ]);
```

### **1.3. Creating Route objects and inserting them**

[](#13-creating-route-objects-and-inserting-them)

Create a Route object and call `Router::addRoutes` providing it as an argument. You may insert as many Routes as you want at one call, just separate them with a comma. eg.

```
    use SunnyFlail\Router\Route;
    $routeOne = new Route("index", "/", fn() => print("Hello world, from index!"));
    $routeTwo = new Route(
        "offer", "/offer/{id}",
        fn($id) => printf("Hello world, from offer number %s!", $id),
        ["GET", "HEAD"],
        ["id" => "\d+"],
        ["id" => 1]
    );
    $router->addRoutes($routeOne, $routeTwo);
```

### **1.4. Manually inserting Route data**

[](#14-manually-inserting-route-data)

You may also manually insert the data by calling `Router::addRoute` method. It's syntax is the same as when creating new Route object eg.

```
    $router->addRoute("login", "/login/", [LoginController::class, "login"]);
```

**2. Routing**
--------------

[](#2-routing)

To begin routing invoke *`Router::match`*, providing uppercase *HTTP request method* as first argument, and *request path* as second. eg.

```
    $matchedData = $router->match($_SERVER["REQUEST_METHOD"], $_SERVER["REQUEST_PATH"]);
```

This method returns an instance of *`SunnyFlail\Router\MatchedData`* class containing matched Route and data scraped from argumented segments.

To get these data invoke *`MatchedData::getData`* method. It returns an associative array with parameter names as keys, and scraped values corresponding to the key. It may return an empty array if there were no argumented params

To get matched route, invoke *`MatchedData::getRoute`*

The *`Router::match`* method may throw a *`SunnyFlail\Router\NotFoundException`* if no routes were matched

**3. Operating on inserted Routes**
-----------------------------------

[](#3-operating-on-inserted-routes)

### **3.1. Getting a Route object from Router**

[](#31-getting-a-route-object-from-router)

To get a route call *`Router::getRoute`*, providing the name of searched route as an argument. eg.

```
    $route = $router->getRoute("offer");
```

### **3.2. Generating urls**

[](#32-generating-urls)

To generate relative (to the domainname) url pointing to selected route call *`Route::generateUrl`* method. You may need to provide an associative array to fill the parameters eg.

```
    $url = $route->generateUrl(["id" => 2]);
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

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

Recently: every ~11 days

Total

6

Last Release

1748d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/50b47ac5307f9f924ae62916c792bbfcabe962da1a0eaf68230b2199d4a6283f?d=identicon)[SunnyFlail](/maintainers/SunnyFlail)

---

Top Contributors

[![SunnyFlail](https://avatars.githubusercontent.com/u/82274752?v=4)](https://github.com/SunnyFlail "SunnyFlail (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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