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

ActiveLibrary[Framework](/categories/framework)

anktx/router
============

Simple PHP router

1.0.0(1y ago)02↑2900%PHPPHP ^8.4

Since Apr 29Pushed 1y ago1 watchersCompare

[ Source](https://github.com/anktx/router)[ Packagist](https://packagist.org/packages/anktx/router)[ RSS](/packages/anktx-router/feed)WikiDiscussions master Synced yesterday

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

Anktx Router
============

[](#anktx-router)

Anktx Router is a simple PHP router for dispatching HTTP requests to the appropriate route handlers based on the request method and URL. It supports multiple HTTP methods and path parameters, providing a clean and intuitive way to handle routing in PHP applications.

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

[](#installation)

To use Anktx Router in your project, simply user the following command:

```
composer require anktx/router
```

Usage Example
-------------

[](#usage-example)

Here's a basic example of how to use the `Router` and `Route` classes to handle HTTP requests:

### 1. Create Routes

[](#1-create-routes)

You can define routes using the static methods provided in the `Route` class.

```
use Anktx\Router\Route;

$route1 = Route::get('/home', fn() => 'Welcome to the homepage!');
$route2 = Route::post('/submit', fn() => 'Form submitted!');
$route3 = Route::get('/profile/', fn ($id) => "Profile ID: $id");
$route4 = Route::get('/user/', fn ($name) => "User name: $name");
```

### 2. Create a Locator

[](#2-create-a-locator)

Next, create a `ManualLocator` that contains all the routes you defined.

```
use Anktx\Router\Locator\ManualLocator;

$locator = new ManualLocator($route1, $route2, $route3, $route4);
```

### 3. Dispatch the Request

[](#3-dispatch-the-request)

Once you have your routes set up and the locator created, you can instantiate the `Router` and dispatch requests based on method and URL.

```
use Anktx\Router\Router;
use Anktx\Router\Result\RouteFound;
use Anktx\Router\Result\RouteNotFound;
use Anktx\Router\Result\MethodNotAllowed;

$router = new Router($locator);

// Example 1: Dispatching a GET request
$response = $router->dispatch('GET', '/home');
handleResponse($response);

// Example 2: Dispatching a POST request
$response = $router->dispatch('POST', '/submit');
handleResponse($response);

// Example 3: Dispatching a GET request with a path parameter
$response = $router->dispatch('GET', '/user/123');
handleResponse($response);

function handleResponse(RouteFound|RouteNotFound|MethodNotAllowed $response): void
{
    if ($response instanceof RouteFound) {
        echo "Route found, handler: " . $response->handler() . "\n";
    } elseif ($response instanceof RouteNotFound) {
        echo "Route not found.\n";
    } elseif ($response instanceof MethodNotAllowed) {
        echo "Method not allowed. Allowed methods: " . implode(', ', $response->allowedMethods()) . "\n";
    }
}
```

### 4. Handling the Response

[](#4-handling-the-response)

The `dispatch` method will return an instance of one of the following:

- `RouteFound`: If the route is found and the method is allowed, the handler will be called.
- `RouteNotFound`: If no matching route is found.
- `MethodNotAllowed`: If a route is found, but the method is not allowed (e.g., trying to `POST` to a `GET` route).

Each response type provides useful information, such as the allowed methods in case of a `MethodNotAllowed` response.

### 5. Using Path Parameters

[](#5-using-path-parameters)

In the example above, the route `/user/` uses a path parameter. You can access the matched parameters in the handler.

```
// This will match the URL `/user/123`
// The handler will receive $id = 123
$route3 = Route::get('/user/', fn ($id) => "User ID: $id");
```

### 6. Additional Methods

[](#6-additional-methods)

You can define additional HTTP methods such as `PUT`, `DELETE`, `PATCH`, etc., using similar syntax.

```
Route::put('/update', fn() => 'Update successful');
Route::delete('/delete', fn() => 'Delete successful');
Route::patch('/modify', fn() => 'Modification successful');
```

### Error Handling

[](#error-handling)

If no route matches, or if the method is not allowed, appropriate responses will be returned as shown above. You can also customize these responses based on your application’s needs.

License
-------

[](#license)

This package is open-source software, licensed under the MIT License.

###  Health Score

29

—

LowBetter than 58% of packages

Maintenance46

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity54

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

406d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7415f6a14a4b4ce4c04c84b6e1e9b7ca84b5ea6b899abff07f57d0632a911e08?d=identicon)[anktx](/maintainers/anktx)

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[laravel/socialite

Laravel wrapper around OAuth 1 &amp; OAuth 2 libraries.

5.7k104.3M822](/packages/laravel-socialite)[laravel/dusk

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

1.9k38.6M289](/packages/laravel-dusk)[pinguo/php-msf

Pinguo Micro Service Framework For PHP

1.7k4.2k](/packages/pinguo-php-msf)[nineinchnick/edatatables

Grid widget for the Yii Framework, wrapper for the DataTables jQuery plugin

173.2k](/packages/nineinchnick-edatatables)

PHPackages © 2026

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