PHPackages                             phower/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. [API Development](/categories/api)
4. /
5. phower/router

ActiveLibrary[API Development](/categories/api)

phower/router
=============

PHP routing package compliant with PSR-7.

1.0.0(9y ago)016MITPHPPHP ^5.6 || ^7.0

Since Sep 18Pushed 9y ago1 watchersCompare

[ Source](https://github.com/phower/router)[ Packagist](https://packagist.org/packages/phower/router)[ RSS](/packages/phower-router/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (5)Versions (2)Used By (0)

Phower Router
=============

[](#phower-router)

PHP routing package compliant with [PSR-7](http://www.php-fig.org/psr/psr-7/).

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

[](#requirements)

Phower Router requires:

- [PHP 5.6](http://php.net/releases/5_6_0.php) or above; version [7.0](http://php.net/releases/7_0_0.php) is recommended

Instalation
-----------

[](#instalation)

Add Phower Router to any PHP project using [Composer](https://getcomposer.org/):

```
composer require phower/router
```

Usage
-----

[](#usage)

Routing is the process to match and drive an HTTP request to a previously given route. This process requires a Router instance with one or more Routes to evaluate against the request.

### Routes

[](#routes)

Each Route must have a name and a definition. Optionally it may also have constraints, default values for optional segments and allowed methods.

A simple Route example to match requests to the root path (`/`):

```
use Phower\Router\Route;

$route = new Route('home', '/');
```

Another example for a Route matching requests to `/some/path` path, with a required `id` argument and an optional `name` argument:

```
use Phower\Router\Route;

$route = new Route('some-name', '/some/path/:id[/:name]');
```

> Note that arguments are preceeded with a colon (`:`) and to make them optional we must use squared brackets.

To force an argument to some pattern we can specify a third argument in form of an associative array containing a regular expression for each constraint.

In previous example we may wish to constrain `id` argument to only accept digits:

```
use Phower\Router\Route;

$route = new Route('some-name', '/some/path/:id[/:name]', ['id' => '\d+']);
```

We can also provide default values for optional arguments. In case they are not specified in the the original request then the default value will take their place. It is also possible to attach other values to a Route using the defaults array:

```
use Phower\Router\Route;

$route = new Route('some-name', '/some/path/:id[/:name]', ['id' => '\d+'], [
    'name' => 'Phower', // default value for name argument
    'type' => 'route',  // aditional value attached to this Route
]);
```

Some routes are also expected to match against a request with a specific method. For example to just match `POST` requests we can tell that using a fifth argument in the Route signature

```
use Phower\Router\Route;

$route = new Route('some-name', '/some/path/:id[/:name]', ['id' => '\d+'], [
    'name' => 'Phower', // default value for name argument
    'type' => 'route',  // aditional value attached to this Route
], 'POST');
```

A static factory method is available to create routes from a configuration array:

```
use Phower\Router\Route;

$route = Route::factory([
    'name' = > 'some-name',
    'definition' = > '/some/path/:id[/:name]',
    'constraints' = > [
        'id' => '\d+',
    ],
    'defaults' = > [
        'name' => 'Phower',
        'type' => 'route',
    ],
    'methods' = > 'POST',
]);
```

Assembling a Route instance means to build its URL from the required arguments:

```
use Phower\Router\Route;

$route = Route::factory([
    'name' = > 'some-name',
    'definition' = > '/some/path/:id[/:name]',
    'constraints' = > [
        'id' => '\d+',
    ],
    'defaults' = > [
        'name' => 'Phower',
        'type' => 'route',
    ],
    'methods' = > 'POST',
]);

$url = $route->assemble([
    'id' => 123,
    'name' => 'my-name',
]);
// $url equals to: "/some/path/123/my-name"
```

### Router

[](#router)

Router is a stack of routes with methods to match a given HTTP request and to assemble URLs for a named route.

Creating a Router instance just requires an array containing weither Route instances or configuration arrays to instantiate routes:

```
use Phower\Router\Router;
use Phower\Router\Route;

$router = new Router([
    new Route('home', '/'),
    [
        'name' => 'page',
        'definition' => '/page',
    ],
]);
```

Matching an HTTP request requires an instance of [RequestInterface](https://github.com/php-fig/http-message/blob/master/src/RequestInterface.php)as argument:

```
use Phower\Router\Router;
use Phower\Router\Route;

$router = new Router([
    new Route('home', '/'),
    [
        'name' => 'page',
        'definition' => '/page',
    ],
]);

/* @var $request \Psr\Http\Message\RequestInterface */
if ($router->match($request)) {
    $matchedRoute = $router->getMatched();
}
```

We can also assemble a route by name from a Router instance containing that named route:

```
use Phower\Router\Router;
use Phower\Router\Route;

$router = new Router([
    new Route('home', '/'),
    new Route('profiles', '/profiles'),
    new Route('profile', '/profiles/:id'),
]);

$url = $router->assemble('profiles', ['id' => 123]);
// $url equals to: "/profiles/123"
```

Running Tests
-------------

[](#running-tests)

Tests are available in a separated namespace and can run with [PHPUnit](http://phpunit.de/)in the command line:

```
vendor/bin/phpunit
```

Coding Standards
----------------

[](#coding-standards)

Phower code is written under [PSR-2](http://www.php-fig.org/psr/psr-2/) coding style standard. To enforce that [CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) tools are also provided and can run as:

```
vendor/bin/phpcs
```

Reporting Issues
----------------

[](#reporting-issues)

In case you find issues with this code please open a ticket in Github Issues at .

Contributors
------------

[](#contributors)

Open Source is made of contribuition. If you want to contribute to Phower please follow these steps:

1. Fork latest version into your own repository.
2. Write your changes or additions and commit them.
3. Follow PSR-2 coding style standard.
4. Make sure you have unit tests with full coverage to your changes.
5. Go to Github Pull Requests at and create a new request.

Thank you!

Changes and Versioning
----------------------

[](#changes-and-versioning)

All relevant changes on this code are logged in a separated [log](CHANGELOG.md) file.

Version numbers follow recommendations from [Semantic Versioning](http://semver.org/).

License
-------

[](#license)

Phower code is maintained under [The MIT License](https://opensource.org/licenses/MIT).

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

3572d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/04e91a806958f09ca8b3f4c24c6f5c4bcbce3c4e44dd882fa1e4c737ee7774c6?d=identicon)[pedrobrazao](/maintainers/pedrobrazao)

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.2k532.1M2.5k](/packages/aws-aws-sdk-php)[algolia/algoliasearch-client-php

API powering the features of Algolia.

69734.4M145](/packages/algolia-algoliasearch-client-php)[telnyx/telnyx-php

Official Telnyx PHP SDK — APIs for Voice, SMS, MMS, WhatsApp, Fax, SIP Trunking, Wireless IoT, Call Control, and more. Build global communications on Telnyx's private carrier-grade network.

35729.6k2](/packages/telnyx-telnyx-php)[neuron-core/neuron-ai

The PHP Agentic Framework.

2.0k496.1k34](/packages/neuron-core-neuron-ai)[n1ebieski/ksef-php-client

PHP API client that allows you to interact with the API Krajowego Systemu e-Faktur

8754.6k](/packages/n1ebieski-ksef-php-client)[trycourier/courier

Courier PHP SDK

15654.8k](/packages/trycourier-courier)

PHPackages © 2026

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