PHPackages                             asko/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. [Framework](/categories/framework)
4. /
5. asko/router

ActiveLibrary[Framework](/categories/framework)

asko/router
===========

A minimal PHP router with configuration-free dependency injection

v3.0(1y ago)132831[1 issues](https://github.com/askonomm/router/issues)MITPHPPHP &gt;=8.2

Since Sep 9Pushed 1y ago2 watchersCompare

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

READMEChangelog (8)Dependencies (1)Versions (9)Used By (0)

Router
======

[](#router)

[![codecov](https://camo.githubusercontent.com/7835f2f04ab70469be7b311924bb0b622321429fea7bf852a4c6fdf049a43915/68747470733a2f2f636f6465636f762e696f2f67682f61736b6f6e6f6d6d2f726f757465722f67726170682f62616467652e7376673f746f6b656e3d3755544a4e31544b3653)](https://codecov.io/gh/askonomm/router)

A PHP router with built-in configuration-free dependency injection.

Install
-------

[](#install)

```
composer require asko/router

```

Usage
-----

[](#usage)

The most basic usage example looks like this:

```
use Asko\Router\Router;

$router = new Router();

$router->get("/hello/{who}", function(string $who) {
    echo "Hello, {$who}";
});

$router->dispatch();
```

All HTTP methods are supported: `get`, `head`, `post`, `put`, `delete`, `patch`, `options`, `trace`. To catch any HTTP method, use `any`.

To set a 404 handler, use the `not_found` method, which just takes a callable as its only argument.

### Callables

[](#callables)

You can pass 3 different types of callables to the router.

#### Controllers

[](#controllers)

Controllers are classes which are mostly used for grouping routes that are similar together, so let's say you have Admin page routes, well, it would make sense to create a `AdminController` class for that, where each method in it represents a single route. To use controller classes with Router you need to pass an array as the callable, where the first item is the class constant and the second the name of the method, like so:

```
use Asko\Router\Router;

class AdminController
{
    public function login()
    {
        echo "Login page goes here.";
    }
}

$router = new Router();
$router->get("/admin/login", [AdminController::class, "login"]);
$router->dispatch();
```

#### Functions

[](#functions)

Functions are regular PHP functions where you pass the name of the function as the callable. This is useful if you want to do more functional style programming, like so:

```
use Asko\Router\Router;

function hello_world() {
    echo "Hello, World!";
}

$router = new Router();
$router->get("/hello-world", "hello_world");
$router->dispatch();
```

#### Closures

[](#closures)

You can also entirely forgo having named functions and instead use anonymous functions in the form of Closures, like so:

```
use Asko\Router\Router;

$router = new Router();

$router->get("/hello-world", function() {
    echo "Hello, World!";
});

$router->dispatch();
```

### Parameters

[](#parameters)

Parameters in Router are named, and then used in the function (or method) declaration as arguments with those same names. So let's say you have this Route:

```
$router->get("/hello/{who}", ...);
```

Then the argument name you need to refer to is also `$who`, like so:

```
$router->get("/hello/{who}", function(string $who) {
    echo "Hello, {$who}!";
});
```

You can have as many parameters as you wish, and the order of which you have them in the function declaration does not matter. The only thing that matters is that the name matches the Route parameter.

**Note:** your parameters must be type hinted as either `string`, `int` or `float`. Leaving parameters untyped will assume the parameter is `string`.

### Dependency injection

[](#dependency-injection)

Router has configuration-free dependency injection in the form of type hinting classes in the function (or method) declaration. Dependency injections must occur before Route parameters. An example injection looks like this:

```
use Asko\Router\Router;

class SomeDependency {}

$router = new Router();

$router->get("/hello/{who}", function(SomeDependency $dep, string $who) {
    echo "Hello, {$who}";
});

$router->dispatch();
```

The above examples instantiates the `SomeDependency` class and injects it into the callable. All callable types are supported: Controller methods (and constructor methods!), functions and Closures.

All injections are also recursive in nature, which means that the injected classes can also benefit from configuration-free dependency injection by type hinting injections in their respective constructor methods.

### Middlewares

[](#middlewares)

Middlewares are a way to run code before the actual route is dispatched. You can use middlewares to check if a user is authenticated, or if a user has the right permissions to access a route, etc. Middlewares are added to the router by using the `middleware` method, like so:

```
use Asko\Router\Router;

class SomeMiddleware
{
    public function handle(string $who): string
    {
        return "intercepted, {$who}!";
    }
}

$router = new Router();

$router->get(
    path: '/hello/{who}',
    callable: fn(string $who) => "Hello, {$who}!",
    middlewares: [SomeMiddleware::class]
});
```

When the above route is dispatched, the `SomeMiddleware` class will be instantiated and the `handle` method will be called. If the `handle` method returns anything other than `null`, the route will not be dispatched and the return value of the `handle` method will be returned instead.

The `handle` method of a middleware also fully supports dependency injection, and can make use of the same parameters as in the route itself.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity61

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

Recently: every ~64 days

Total

8

Last Release

717d ago

Major Versions

v1.2 → v2.02023-09-17

v2.2.0 → v3.02024-05-30

### Community

Maintainers

![](https://www.gravatar.com/avatar/1610a37b5e2e2ae8c28051865e6a61fcebb450c3908aedf0a8501bd6967f9da8?d=identicon)[asko](/maintainers/asko)

---

Top Contributors

[![askonomm](https://avatars.githubusercontent.com/u/84135165?v=4)](https://github.com/askonomm "askonomm (27 commits)")

---

Tags

dependency-injectionphprouter

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

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