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

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

math280h/php-router
===================

Simple PHP Router

v1.1.0(3y ago)113Apache-2.0PHP

Since Feb 15Pushed 3y ago1 watchersCompare

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

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

php-router
==========

[](#php-router)

Simple PHP Router, it supports features such as middlewares and views using aura/view

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

[](#installation)

```
composer require math280h/php-router

```

Usage
-----

[](#usage)

For simple usage you can inline callback functions directly in the routes as shown below.

```
use Math280h\PhpRouter\Router;
use Math280h\PhpRouter\Request;

session_start();

$router = new Router("./public");
$router->get('/', function () {
    echo 'Hello World';
});

$router->post('/', function (Request $request) {
    echo 'Hello World';
});

$router->run();
```

***Note**: The router always passes the Request object to callback functions*

### Accepting different HTTP Methods

[](#accepting-different-http-methods)

The router exposes a function for each accepted HTTP method. The list of available methods are:

- GET
- POST
- PUT
- DELETE
- OPTIONS
- HEAD
- PATCH

And they can be called like so:

```
$router->get('/', MyController::class . '::index')
$router->post('/', MyController::class . '::index')
```

### Route groups

[](#route-groups)

You can group routes together, this allows you to e.g. define a prefix for a colleciton of routes or apply middleware to the collection of routes.

You can define a route group like so:

```
use Math280h\PhpRouter\Route;

$router->group(["prefix" => "test"], [
    Route::get('/1', function () {
        echo 'Hello World';
    }),
    Route::get('/2', function () {
        echo 'Hello World';
    }),
]);
```

This example results in two routes with the paths `/test/1` and `/test/2`

### Adding middleware

[](#adding-middleware)

The router supports middleware than runs after the connection is accepted but before the request is forwarded to the callback function.

For now, only direct callback functions are supported and can be added to the router like so:

```
$router->addMiddleware("log", function ($request) {
    print_r($request);
});
```

The router will always pass the request object to the callback function.

Once the middleware is added to the router you can attach it to your route like so:

```
$router->get('/', function () {
    echo 'Hello World';
}, ['log']);
```

### Passing callbacks from classes to the router

[](#passing-callbacks-from-classes-to-the-router)

The router allows you to pass references to functions inside classes instead of inlining the callback functions You can do this like so:

```
class MyController {
    public function index($request) {
        echo 'Hello World';
    }
}

$router->get('/', MyController::class . '::index');
```

### Returning views

[](#returning-views)

The router is built with support for aura/view. This allows callback functions to pass back a view and the router will automatically render it.

This can achieved like so:

```
class MyController {
    public function index($request) {
        $view_factory = new \Aura\View\ViewFactory;
        $view = $view_factory->newInstance();
        $view_registry = $view->getViewRegistry();
        $layout_registry = $view->getLayoutRegistry();
        $layout_registry->set('default', dirname(__DIR__) . '/views/layouts/default.php');
        $view_registry->set('page', dirname(__DIR__) . '/views/my-view.php');
        $view->setView('page');
        $view->setLayout($layout);
        return $view;
    }
}

$router->get('/', MyController::class . '::index');
```

It's recommended to implement a helper function for spinning up new views so you don't have to duplicate the factory creation in your code

#### View helper function

[](#view-helper-function)

A view helper function can look something like this:

```
/**
 * Returns a view Object
 *
 * @param string $path
 * @param array $data
 * @param string $layout
 * @return View
 */
function view(string $path, array $data = [], string $layout = 'default'): View
{
    $view_factory = new \Aura\View\ViewFactory;
    $view = $view_factory->newInstance();
    $view_registry = $view->getViewRegistry();
    $layout_registry = $view->getLayoutRegistry();
    $layout_registry->set('default', dirname(__DIR__) . '/views/layouts/default.php');
    $view_registry->set('page', dirname(__DIR__) . '/views/' . $path . '.php');
    $view->setView('page');
    $view->setLayout($layout);
    $view->addData($data);
    return $view;
}
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

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

Total

2

Last Release

1186d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/20e1316f41e9be00ec9e2734a50fd09c8e12fb8b241147dff63530387ae89d7c?d=identicon)[math280h](/maintainers/math280h)

---

Top Contributors

[![math280h](https://avatars.githubusercontent.com/u/1547127?v=4)](https://github.com/math280h "math280h (11 commits)")

---

Tags

phpphp-libraryphp-routerphp-routingrouter

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

PHPackages © 2026

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