PHPackages                             pijush\_gupta/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. pijush\_gupta/php-router

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

pijush\_gupta/php-router
========================

Simple Php Router

02PHP

Since Apr 7Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/Pijushgupta/php-router)[ Packagist](https://packagist.org/packages/pijush_gupta/php-router)[ RSS](/packages/pijush-gupta-php-router/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

PHP Router
==========

[](#php-router)

A lightweight, high-performance PHP routing engine with support for static routes, dynamic parameters, and multiple HTTP verbs. Written with modern PHP practices and elite performance in mind using Grouped Regex and Hash Map matching.

Features
--------

[](#features)

- **Blazing Fast**: `O(1)` Hash Map lookups for static routes and Grouped Regex for dynamic routes.
- **Dynamic Parameters**: Support for segments like `/user/{id}`.
- **HTTP Verbs**: Explicit `GET` and `POST` methods.
- **PSR-4 Compliant**: Ready for Composer autoloading out of the box.
- **Custom 404 Handling**: Easily define your own "Not Found" pages.

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

[](#installation)

You can install the router via Composer:

```
composer require pijush_gupta/php-router
```

If you are not using Composer, simply require the file:

```
require_once 'src/Router.php';
```

User Guide
----------

[](#user-guide)

### 1. Basic Routing

[](#1-basic-routing)

Define basic routes using static paths.

```
use phpRouter\Router;

Router::get('/', function() {
    echo 'Welcome Home!';
});

Router::post('/submit', function($postData) {
    print_r($postData);
});
```

### 2. Dynamic Routing

[](#2-dynamic-routing)

Use curly braces `{}` to define dynamic segments in your URLs. Let the router extract these and pass them directly to your callback.

```
Router::get('/user/{id}', function($id) {
    echo "Viewing profile for user ID: " . $id;
});

Router::get('/post/{category}/{slug}', function($category, $slug) {
    echo "Category: $category, Slug: $slug";
});
```

### 3. Controller Callbacks

[](#3-controller-callbacks)

You don't have to use closures. You can use standard PHP callbacks, like an array containing an object and a method name.

```
class UserController {
    public function showProfile($id) {
        echo "Profile of $id";
    }
}

$controller = new UserController();
Router::get('/profile/{id}', [$controller, 'showProfile']);
```

### 4. Handling Query Parameters

[](#4-handling-query-parameters)

For `GET` requests, query parameters are automatically gathered and passed to your closure if defined.

```
// URL: /search?query=php&sort=asc
Router::get('/search', function($query, $sort) {
    echo "Searching for $query ordered by $sort.";
});
```

### 5. Custom 404 Pages

[](#5-custom-404-pages)

Set a custom handler that triggers if no route matches the requested URI.

```
Router::setNotFound(function() {
    http_response_code(404);
    echo "404 - Not Found";
    echo "The page you are looking for does not exist.";
});
```

### 6. Dispatching the Router

[](#6-dispatching-the-router)

By default, the router will automatically dispatch routes when the script terminates via its destructor. However, you can explicitly dispatch it anywhere in your code lifecycle:

```
Router::dispatch();
```

License
-------

[](#license)

This project is open-sourced software licensed under the [MIT license](LICENSE).

###  Health Score

19

—

LowBetter than 9% of packages

Maintenance56

Moderate activity, may be stable

Popularity2

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/c74fdb54e7d0980abb730547ee47bf48a90ed2bb616b0fa66186d7672c9c7356?d=identicon)[Pijushgupta](/maintainers/Pijushgupta)

---

Top Contributors

[![Pijushgupta](https://avatars.githubusercontent.com/u/1472227?v=4)](https://github.com/Pijushgupta "Pijushgupta (17 commits)")

### Embed Badge

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

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

###  Alternatives

[lodge/postcode-lookup

A postcode lookup utility using the Google Maps API, ready to use with Laravel 5.

3430.5k](/packages/lodge-postcode-lookup)[danielsiepmann/tracking

Tracking for TYPO3

1831.1k](/packages/danielsiepmann-tracking)[robertlemke/plugin-blog

A blog plugin for Neos

293.4k1](/packages/robertlemke-plugin-blog)

PHPackages © 2026

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