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

ActiveLibrary[Framework](/categories/framework)

hostmyanmar/php-router
======================

A simple and lightweight PHP router package.

v1.0.0(1y ago)00MITPHPPHP ^8.1

Since May 23Pushed 1y agoCompare

[ Source](https://github.com/22HMM/php-router)[ Packagist](https://packagist.org/packages/hostmyanmar/php-router)[ RSS](/packages/hostmyanmar-php-router/feed)WikiDiscussions main Synced 3w ago

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

hostmyanmar/php-router
======================

[](#hostmyanmarphp-router)

[![Version](https://camo.githubusercontent.com/c1acccb8330605b9a6c4aafe892c66b12cb2a7a5c511b5abef22cb4d6f045d33/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f686f73746d79616e6d61722f7068702d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hostmyanmar/php-router)[![Downloads](https://camo.githubusercontent.com/90d2b6d3c45efe47f42fe92deb8f20a342d53f00594d8f7b10ffd62b49037a44/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686f73746d79616e6d61722f7068702d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hostmyanmar/php-router/stats)[![License](https://camo.githubusercontent.com/113a524d46affab8d258a8014088df52b0d9bbe60c36a52e62bea94c8eb59bfb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f686f73746d79616e6d61722f7068702d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://github.com/hostmyanmar/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 hostmyanmar/php-router

```

Usage
-----

[](#usage)

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

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

use \HostMyanmar\PhpRouter\Router;

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

// Define routes
// ...

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

### 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;start(); 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

26

—

LowBetter than 41% of packages

Maintenance47

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

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

Unknown

Total

1

Last Release

397d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/33fbebda290b5e394a7f84b44d2a128087ed154867bcd03283185364dc423ab8?d=identicon)[Host Myanmar](/maintainers/Host%20Myanmar)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[laravel/socialite

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

5.7k104.3M829](/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)[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)
