PHPackages                             onesimus-systems/osrouter - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. onesimus-systems/osrouter

ActiveLibrary[HTTP &amp; Networking](/categories/http)

onesimus-systems/osrouter
=========================

A simple, easy to use router for PHP web applications

1.5.0(6y ago)1138[1 PRs](https://github.com/onesimus-systems/osrouter/pulls)BSD-3-ClausePHPCI passing

Since Jun 18Pushed 3mo agoCompare

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

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

OSRouter
--------

[](#osrouter)

OSRouter is a simple, fast HTTP router for PHP. It can perform simple routes with variable and optional URL parts. OSRouter can extract parts of a URL as variable that can be passed to a closure or a controller method.

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

[](#requirements)

- PHP &gt;= 5.4.0

Usage
-----

[](#usage)

```
use \Onesimus\Router\Router;
use \Onesimus\Router\Http\Request;

// First we add some routes
// The root path is sent to the index method of the HomeController class
Router::get('/', 'HomeController@index');
// Any path with a page such as /dash, /admin, etc
Router::get('/{page}', 'RenderController@render');
// API, required module, optional method
// Executes the closure
Router::post('/api/{module}/{?method}', function() {
	// Closure for route
	return;
});
// Match rest of URL
Router::get('/api/{*rest}', 'ApiController@serve');

// Get request object for current HTTP request
$request = Request::getRequest();
// Get the matching route for the given request
$route = Router::route($request);
// Dispatch route
// Execute the closure or the class/method combo
$route->dispatch();

// You can also pass an argument to the called closure
// or the class being instantiated
$route->dispatch($app);
```

The main methods are `Router::get()`, `::post()`, and `::any()`. The signature for each is `($pattern, $callback, $options = [])`. $pattern is the URI pattern the route will match using the format `/static/{variable}/{?option-variable}`. $callback is either a string such as `Controller@method` or a closure function. $options can be a single string in which case it will interpreted as a single filter, or it can be an array with the syntax `['filter' => ['filter1', 'filter2']]`. The outer array is to allow for extra options that may be added later on.

To route in an application located in a subdirectory of the webserver, you'll need to remove the directory prefix from the REQUEST\_URI field in the Request object. For example, if the base of your application was located at `http://example.com/blog`, you will need to strip "/blog" from the request uri before processing a route. Otherwise, all routes will be checked against "/blog/something" instead of just "/something".

You can also register routes in a group

```
Router::group(['prefix' => '/admin'],
	['get', '/groups/{?id}', 'Controller@groups'],
	['get', '/users/{?id}', 'Controller@user']
);
```

Groups are defined using the `Router::group(array $options, array $routes)` method. $options is a keyed array with the keys 'filter', 'prefix', and 'rprefix'. 'Filter' is an array of filters that apply to the group. A single filter can be given as a string as well. 'Prefix' is a prefix added to the HTTP pattern in each group. In the example above, the routes will be `/admin/groups/{?id}` and `/admin/users/{?id}`. 'Rprefix' is prepended to each controller statement. For example, if `'rprefix' => '\Namespace\Admin\'` was added to the group above, the controller statements would be `\Namespace\Admin\Controller@groups` and `\Namespace\Admin\Controller@user`.

**Note**: Closures cannot be assigned to a route defined in a group. To assign a closure, assign the route separately.

You can define a 404 route using the method `Router::register404Route($callback, $options = [])`. If no 404 route is defined and a route isn't found, a `RouteException` will be thrown.

Filters
-------

[](#filters)

Filters can be defined and assigned to routes. Routes can have multiple filters, they will be handled in the order they're defined on the route. If all filters return a non-falsey value, the route will be green-lit and the dispatch will continue. Otherwise a `FailedFilterException` will be thrown. Also, a `RouteException` will be thrown if the filter isn't defined.

Example:

```
// First register filter
// This function will return true if the session is authenticated
// or false otherwise. You may want to do some sort of redirect here
// as well for a failed authentication for example to a login page.
Router::filter('auth', function() {
    return is_authenticated();
});

// Define route for '/admin' path with filter of 'auth'
Router::get('/admin', 'AdminController@index', 'auth');
```

When the above route is dispatched, the 'auth' filter will be executed and if it returns a non-falsey value, the AdminController will be given control as normal. You can define multiple filters using an array.

```
Router::get('/admin', 'AdminController@index', ['auth', 'inAdminGroup']);
```

In this case both filters must return truthy.

Router\\Http\\Request
---------------------

[](#routerhttprequest)

Router\\Http\\Response
----------------------

[](#routerhttpresponse)

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance53

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity71

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

Recently: every ~75 days

Total

13

Last Release

2233d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8b0c8b2c3cadee570c224131bb905fcd14df14a41d0100796509d6a9df402618?d=identicon)[dragonrider23](/maintainers/dragonrider23)

---

Top Contributors

[![lfkeitel](https://avatars.githubusercontent.com/u/6619743?v=4)](https://github.com/lfkeitel "lfkeitel (33 commits)")

---

Tags

httprouter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/onesimus-systems-osrouter/health.svg)

```
[![Health](https://phpackages.com/badges/onesimus-systems-osrouter/health.svg)](https://phpackages.com/packages/onesimus-systems-osrouter)
```

###  Alternatives

[middlewares/fast-route

Middleware to use FastRoute

96191.1k15](/packages/middlewares-fast-route)[miladrahimi/phprouter

A powerful, lightweight, and very fast HTTP URL router for PHP projects.

20832.6k2](/packages/miladrahimi-phprouter)[sunrise/http-router

A powerful solution as the foundation of your project.

16249.8k10](/packages/sunrise-http-router)[amphp/http-server-router

Routes to request handlers based on HTTP method and path for amphp/http-server.

39496.2k28](/packages/amphp-http-server-router)[aphiria/aphiria

The Aphiria framework

1427.7k2](/packages/aphiria-aphiria)[wilaak/radix-router

High-performance radix tree based HTTP request router

612.8k5](/packages/wilaak-radix-router)

PHPackages © 2026

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