PHPackages                             hexbit/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. hexbit/router

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hexbit/router
=============

Wordpress Router

1.1.1(4y ago)352.8k↓100%4[1 issues](https://github.com/smarteist/wordpress-router/issues)MITPHPPHP ^7.2|^8.0CI failing

Since May 29Pushed 4y ago3 watchersCompare

[ Source](https://github.com/smarteist/wordpress-router)[ Packagist](https://packagist.org/packages/hexbit/router)[ Docs](https://github.com/smarteist/wordpress-router)[ RSS](/packages/hexbit-router/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (3)Versions (6)Used By (0)

Wordpress Router
================

[](#wordpress-router)

[![CI](https://camo.githubusercontent.com/97bae0af717233695553df45b4955252fdfb7916ad986a5bffcb10f4c82e7dc4/68747470733a2f2f7472617669732d63692e6f72672f736d617274656973742f776f726470726573732d726f757465722e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/97bae0af717233695553df45b4955252fdfb7916ad986a5bffcb10f4c82e7dc4/68747470733a2f2f7472617669732d63692e6f72672f736d617274656973742f776f726470726573732d726f757465722e7376673f6272616e63683d6d6173746572)

A simple PHP router built on [SymfonyHttpFoundation](https://github.com/symfony/http-foundation) and [AltoRouter](https://github.com/dannyvankooten/AltoRouter) based on laravel API. This library is actually built to make routing easier on the [RootsSage](https://github.com/roots/sage) starter framework, however it can also be used in other systems

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

[](#installation)

```
composer require hexbit/router

```

Usage
-----

[](#usage)

### Creating Routes

[](#creating-routes)

#### Map Methods

[](#map-methods)

Creating a route is done using the `map` function:

In wordpress

```
// for wordpress projects
use Hexbit\Router\WordPress\Router;

// first init router
add_action("init", function () {
     Router::init();
});

// Creates a route that matches the uri `/posts/list` both GET
// and POST requests.
Router::map(['GET', 'POST'], 'posts/list', function () {
    return 'Hello World';
});
```

If you do not use the WordPress system, use the router class below, and [you can attempt to match your current request](#matching-routes-to-requests) in appropriate time.

```
// use this class for non wordpress systems
use Hexbit\Router\Router;

// Creates a route that matches the uri `/posts/list` both GET
// and POST requests.
Router::map(['GET', 'POST'], 'posts/list', function () {
    return 'Hello World';
});
```

`map()` takes 3 parameters:

- `methods` (array): list of matching request methods, valid values:
    - `GET`
    - `POST`
    - `PUT`
    - `PATCH`
    - `DELETE`
    - `OPTIONS`
- `uri` (string): The URI to match against
- `action` (function|string): Either a closure or a Controller string

#### Route Parameters

[](#route-parameters)

Parameters can be defined on routes using the `{keyName}` syntax. When a route matches that contains parameters, an instance of the `RouteParams` object is passed to the action. Second parameter is an instance of `Symfony\Component\HttpFoundation\Request` which contains the information of the current request.

```
Router::map(['GET'], 'posts/{id}', function(RouteParams $params, Request $request) {
    return $params->id;
});
```

#### Named Routes

[](#named-routes)

Routes can be named so that their URL can be generated programatically:

```
Router::map(['GET'], 'posts/all', function () {})->name('posts.index');

$url = Router::url('posts.index');
```

If the route requires parameters you can be pass an associative array as a second parameter:

```
Router::map(['GET'], 'posts/{id}', function () {})->name('posts.show');

$url = Router::url('posts.show', ['id' => 123]);
```

#### HTTP Verb Shortcuts

[](#http-verb-shortcuts)

Typically you only need to allow one HTTP verb for a route, for these cases the following shortcuts can be used:

```
Router::get('test/route', function () {});
Router::post('test/route', function () {});
Router::put('test/route', function () {});
Router::patch('test/route', function () {});
Router::delete('test/route', function () {});
Router::options('test/route', function () {});
```

#### Setting the basepath

[](#setting-the-basepath)

The router assumes you're working from the route of a domain. If this is not the case you can set the base path:

```
Router::setBasePath('base/path');
Router::map(['GET'], 'route/uri', function () {}); // `/base/path/route/uri`
```

#### Controllers

[](#controllers)

If you'd rather use a class to group related route actions together you can pass a Controller String to `map()` instead of a closure. The string takes the format `{name of class}@{name of method}`. It is important that you use the complete namespace with the class name.

Example:

```
// TestController.php
namespace \MyNamespace;

class TestController
{
    public function testMethod()
    {
        return 'Hello World';
    }
}

// routes.php
Router::map(['GET'], 'route/uri', '\MyNamespace\TestController@testMethod');
```

### Creating Groups

[](#creating-groups)

It is common to group similar routes behind a common prefix. This can be achieved using Route Groups:

```
Router::group('api/v1/', function ($group) {
    $group->map(['GET'], 'route1', function () {}); // `/prefix/route1`
    $group->map(['GET'], 'route2', function () {}); // `/prefix/route2§`
});
```

### Matching Routes to Requests

[](#matching-routes-to-requests)

Once you have routes defined, you can attempt to match your current request against them using the `match()` function. `match()` accepts an instance of Symfony's `Request` and returns an instance of Symfony's `Symfony\Component\HttpFoundation\Response`:

```
// bool|Response
$response = Router::match();

// send response if route matches with current request
if ($response && $response->getStatusCode() !== Response::HTTP_NOT_FOUND) {
            $response->send();
            exit();
}
```

If you return an instance of `Response` from your closure it will be sent back un-touched. If however you return something else, it will be wrapped in an instance of `Response` with your return value as the content.

##### on not found

[](#on-not-found)

If no route matches the request, a boolean `false` will be returned as match response.

### Wordpress Virtual Pages

[](#wordpress-virtual-pages)

This feature allows you to create template pages on the fly, and there is no need to have that page in the database.

```
// by default laods page-custom-admin-login.php
$loginPage = new VirtualPage('custom-admin-login', 'Admin Login Title');

Router::virtualPage('login-admin/', $loginPage);
```

Now voila! `http://yoursite.domain/login-admin` loads virtual page template.

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

[](#contributing)

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License
-------

[](#license)

MIT

###  Health Score

35

—

LowBetter than 81% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity64

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

Total

5

Last Release

1602d ago

PHP version history (2 changes)1.0.0PHP ^7.0

1.1.1PHP ^7.2|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/11ea0610008f6712c8c0466e4057fbb3a6a362530fd48786eb175701229587b9?d=identicon)[smarteist](/maintainers/smarteist)

---

Top Contributors

[![smarteist](https://avatars.githubusercontent.com/u/30802876?v=4)](https://github.com/smarteist "smarteist (14 commits)")

---

Tags

symfonywordpressrouterhexbitsmarteistaltorouter

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[babdev/pagerfanta-bundle

Bundle integrating Pagerfanta with Symfony

20817.4M58](/packages/babdev-pagerfanta-bundle)[apy/datagrid-bundle

Symfony Datagrid Bundle

492991.8k9](/packages/apy-datagrid-bundle)[elao/accesseo

Provide accessibility and SEO insights of your page in Symfony profiler

299.2k](/packages/elao-accesseo)[hexbit/sage-woocommerce

Woocommerce support for sage 10

256.9k](/packages/hexbit-sage-woocommerce)

PHPackages © 2026

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