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

ActiveLibrary[Framework](/categories/framework)

khantloonthu/php-router
=======================

A simple and lightweight PHP router package.

v1.0.1(1y ago)01MITPHPPHP ^8.1

Since May 23Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/KhantLoonThu/php-router)[ Packagist](https://packagist.org/packages/khantloonthu/php-router)[ RSS](/packages/khantloonthu-php-router/feed)WikiDiscussions master Synced today

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

khantloonthu/php-router
=======================

[](#khantloonthuphp-router)

[![Version](https://camo.githubusercontent.com/31fd981cb5b0ab63efd77a44c635ade53a34112684808e499bd8c2f3a7326f05/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b68616e746c6f6f6e7468752f7068702d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/khantloonthu/php-router)[![Downloads](https://camo.githubusercontent.com/03186eb9105dc4236d657cae21ea3a24b4bdb945c1f935d0c05f6cfc847faea0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b68616e746c6f6f6e7468752f7068702d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/khantloonthu/php-router/stats)[![License](https://camo.githubusercontent.com/188c865786a70591ee5a7fd358232907b84c8dd015847a6711926795e65faadc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6b68616e746c6f6f6e7468752f7068702d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://github.com/khantloonthu/php-router/blob/master/LICENSE)

Table of Contents
=================

[](#table-of-contents)

- [Overview](#overview)
- [Installation](#installation)
- [Usage](#usage)
- [Routing](#routing)
- [Routing with params](#routing-with-params)
- [Global Middleware](#global-middleware)
- [Route-Specific middlewares](#route-specific-middlewares)
- [Aborting](#aborting)
- [Redirecting](#redirecting)
- [Handle custom error pages](#handle-custom-error-pages)

Overview
--------

[](#overview)

This is a lightweight, simple, and extensible HTTP router implemented in PHP designed for basic to medium complexity web applications. It supports route registration, middleware, dynamic route parameters, error handling, and HTTP method overrides. Built by Khant Loon Thu *()*.

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

[](#installation)

Installation is possible using Composer

```
composer require khantloonthu/php-router

```

Usage
-----

[](#usage)

Create an instance of `\KhantLoonThu\PhpRouter\Router`, define some routes onto it, and run it.

```
// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

use \KhantLoonThu\PhpRouter\Router;

// Create Router instance
$router = new Router();

// Define routes
// ...

// Run it!
$router->run();
```

### Routing

[](#routing)

Routing shorthands for single request methods are provided:

```
$router->get('/', function() {
    return "Hello World";
});

$router->get('/about-us', fn() => require __DIR__ . '/about-us.php');

$router->get('/contact-us', function () {
    return require __DIR__ . '/contact-us.php';
});

$router->post('/pattern', function() {
    // ...
});

$router->put('/pattern', function() {
    // ...
});

$router->delete('/pattern', function() {
    // ...
});
```

Note:

- \[Routes must be hooked before $router-&gt;run(); is being called\].
- \[Unregistered routes will trigger 404 - not found\].

### Routing with params

[](#routing-with-params)

```
// Required parameter
$router->get('/user/{id}', function($id) {
    return "User ID: " . $id;
});

// Multiple required parameters
$router->get('/post/{postId}/comment/{commentId}', function($postId, $commentId) {
    return "Post ID: $postId, Comment ID: $commentId";
});
```

Note: If route doesn't passed param, it will trigger 400 - Bad Request.

### Global Middleware

[](#global-middleware)

middlewares accept only `true`, `false`, and `['status' => $statusCode, 'message' => 'Unauthorized']`;

```
function checkLoggedIn($request) {
    global $isLoggedIn;

    if (!$isLoggedIn) {
        return ['status' => 401, 'message' => 'Unauthorized: Please log in.'];
    }

    return true; // continue processing
}

$router->middleware('checkLoggedIn');

// or anonymous function style

$router->middleware(function ($request) {
    global $isLoggedIn;

    if (!$isLoggedIn) {
        return false; // triggers 403 Forbidden by default
    }

    return true;
});
```

### Route-Specific middlewares

[](#route-specific-middlewares)

Route specific middlewares accept only `true`, `false`, and `['status' => $statusCode, 'message' => 'Unauthorized']`;

```
$router->get('/dashboard', function () {
    return require __DIR__ . '/dashboard.php';
}, [
    function checkAdmin() {
        $isAdmin = false;
        if (!$isAdmin) return false;
        return true;
    }
]);
```

### Aborting

[](#aborting)

If we want to abort before rendering we can use $router-&gt;abort($statusCode);

```
$router->post('/create-user', function () use ($router) {
    $isAdmin = false;

    if (!$isAdmin) {
        $router->abort(403);
    }

    // continue with user creation...
});
```

### Redirecting

[](#redirecting)

If we want to redirect after finishing the logic, we can use $router-&gt;redirect($path);

```
$router->post('/send-mail', function () use ($router) {
    // send mail logic
    $mail->send();

    $router->redirect('/');
});
```

### Handle custom error pages

[](#handle-custom-error-pages)

If we want to show Custom Error Pages like `403`, `404`, `500`, we can use $router-&gt;handle($statusCode, callable);

```
$router->handle(404, function() {
    return require __DIR__ . '/404.php';
});
```

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance53

Moderate activity, may be stable

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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

Total

2

Last Release

406d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3b08a9719eba0952eab6eaa71441835348fc69181ff723700d4bac2a8bb3a457?d=identicon)[Khant Loon Thu](/maintainers/Khant%20Loon%20Thu)

---

Top Contributors

[![KhantLoonThu](https://avatars.githubusercontent.com/u/142862466?v=4)](https://github.com/KhantLoonThu "KhantLoonThu (5 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[laravel/dusk

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

1.9k39.6M299](/packages/laravel-dusk)[nineinchnick/edatatables

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

173.2k](/packages/nineinchnick-edatatables)[link-cloud/fast-hyperf

LinkCloud Fast Hyperf

241.2k1](/packages/link-cloud-fast-hyperf)

PHPackages © 2026

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