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

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

koddn/php-router
================

Koddn PHP Router - simple router created for custom PHP Applications, CMS, REST API's with multiple callbacks, middleware

352PHP

Since Jul 3Pushed 5y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Koddn PHP Router
================

[](#koddn-php-router)

Koddn Php router is php class that can easily handle routes in a PHP application, supports multiple callbacks, redirects and send responses to client in JSON.

Website: [Koddn Technologies](https://koddn.com)

Link to Docs: [Koddn Php Router](https://koddn.com/php-router)

Social Media Koddn: [Facebook](https://www.facebook.com/koddn/), [Twitter](https://twitter.com/koddn), [Instagram](https://www.instagram.com/koddn.technologies/)

Social Media Developer: [Facebook](https://www.facebook.com/HarpalSingh11/), [Twitter](https://twitter.com/Harpalsingh_11), [Instagram](https://www.instagram.com/harpal.singh11/)

Features
========

[](#features)

- Manages Routing, get, post, put, delete, any - others
- Redirect pages
- Multiple callbacks
- Next function
- Matches paters
- Send Responses, JSON, TEXT
- Set header status
- Express like router in PHP
- Fast route in PHP
- Can be used as Auth Validator

Install
=======

[](#install)

To install with composer:

```
composer require koddn/php-router

```

Usage
=====

[](#usage)

Get the Koddn Php Router

```
// composer auto loader
require __DIR__ . '/vendor/autoload.php';

use KODDN\ROUTER;

// match get request
ROUTER::get('/',function($req,$res,$next){
    // ..do something
    $res->send("Welcome to Koddn Php Router");
});
```

Usage - method 2
----------------

[](#usage---method-2)

Simply add src/KODDN\_ROUTER.php file in your project;

```
// composer auto loader
require __DIR__ . '/src/KODDN_ROUTER.php';

// match get request
ROUTER::get('/',function($req,$res,$next){
    // ..do something
    $res->send("Thanks");
});
```

Patterns
--------

[](#patterns)

You can use patterns to match the request

- :paramName =&gt; named URL segments that are used to capture the values specified at their position in the URL, begin with colon
- - =&gt; will capture anything

In the below example we are capturing user id from the url

```
// URL => /api/user/111

ROUTER::get('/api/user/:id',function($req,$res){

    $id=$req['params']['id'];
    // $req['params]=>['id'='111']

});
```

In the example we are capturing using

```
// URL => /flight/india-usa

ROUTER::get('/flight/:from-:to',function($req,$res){

    // $req['params]=['from'=>'india','to'=>'usa'];
    $from =$req['params']['from'];
    $to = $req['params']['to'];

});
```

```
// URL => /post-name-apc/123
ROUTER::get('/*/:postID',function($req,$res){

    // $req['params']=['postID'=>'123'];
    $postID =$req['params']['postID'];
    // do something

});
```

Route Handlers
==============

[](#route-handlers)

We can use multiple callback functions to handle the route

```
ROUTER::get('/some-url', function($req,$res,$next){

    // Do something here
    echo "START" ;

    // call Next Callback, control goes to next callback function
    $next();

},function($req,$res){

    echo "END";  // task completed

});
```

Also, if we want the edited request to be referenced in next callback then use &amp;$req as request parameter

```
ROUTER::get('/dashboard', function(&$req,$res,$next){

    // if user authorized
    $req['userID']= "someUserID";

    $next();

},function(&$req){

   // now you can have use the req.userID here as well

});
```

Middleware
==========

[](#middleware)

Sample how we can use it as middleware for authentication

Middlewares can be implemented using "use" method

```
ROUTER::use('/user',function($req,$res,$next){

             ROUTER::get('/me', function ($req, $res, $next) {

            // do something here
                        });
        });

// OR
ROUTER::use('/user',function($req,$res,$next){

        require __DIR__."/routes/user.php"; //
});
```

```
ROUTER::post('/login', function(&$req,$res,$next){

    // do the authorize stuff here
    if(!authorize){
    $res->send("invalid");
    }
    $next();

},function(&$req,$res){

   // do something if authorized
  // grantAccessToSomething

});
```

Redirect
========

[](#redirect)

```
//ROUTER::redirect('/url-to-match', callbackBeforeRedirect, 'redirect to url', $replaceHeaders=false (optional), $redirectCode =301 (optional));
ROUTER::redirect('/url-to-match', function(){/*do some logs*/}, '/new-url', $replaceHeaders =false/*( boolean optional)*/, $redirectCode=301 /*(int optional)*/);
```

Responses
=========

[](#responses)

With Koddn PHP router either you can manually handle responses, or you can use the built-in ones.

```
ROUTER::post('/about', function($req,$res,$next){
 $res->send("About us");
});
```

Send JSON data
--------------

[](#send-json-data)

```
ROUTER::post('/api/user', function($req,$res,$next){
    $userData=['name'=>"Harpal Singh", 'id'=>11];
     $res->json($userData);
});
```

Set Header Status Codes
-----------------------

[](#set-header-status-codes)

```
ROUTER::post('/api/user', function($req,$res,$next){

     $res->setStatus(404)->send('Not Found');
});
```

### Clear cookies

[](#clear-cookies)

```
ROUTER::post('/api/user', function($req,$res,$next){
     // clear all cookies
     $res->clearCookies();

     // clear specific cookies
     $res->clearCookies('nameOfCookie');

     // do something

});
```

### Clear cookies

[](#clear-cookies-1)

```
ROUTER::post('/api/user', function($req,$res,$next){
    // clear Sessions
     $res->clearSession();

     // do something

});
```

### End request

[](#end-request)

It is similar to die();

```
ROUTER::post('/api/user', function($req,$res,$next){
    // clear Sessions
     $res->end();
      // do something

});
```

### Redirect using Response

[](#redirect-using-response)

```
//ROUTER::redirect('redirect to url', $replaceHeaders=false (optional), $redirectCode =301 (optional));
ROUTER::post('/api/user', function($req,$res,$next){
    // clear Sessions
     $res->redirect('/new-url',$replaceHeaders=false /*(optional)*/, $redirectCode =301  /*(optional)*/);
      // do something

});
```

ALL Functions
=============

[](#all-functions)

```
ROUTER::any('/url-to-match',function(&$req,$res,$next){}/*, function(&$req,$res,$next){}*/);
ROUTER::post('/url-to-match',function(&$req,$res,$next){}/*, function(&$req,$res,$next){}*/);
ROUTER::get('/url-to-match',function(&$req,$res,$next){}/*, function(&$req,$res,$next){}*/);
ROUTER::put('/url-to-match',function(&$req,$res,$next){}/*, function(&$req,$res,$next){}*/);
ROUTER::delete('/url-to-match',function(&$req,$res,$next){}/*, function(&$req,$res,$next){}*/);
ROUTER::redirect('/url-to-match', function(){/*do some logs*/}, '/new-url', $replaceHeaders =false/*( boolean optional)*/, $redirectCode=301 /*(int optional)*/);
```

```
ROUTER::post('/url-to-match',function(&$req,$res,$next){
//
//$req = ['$fullUrl' => $fullUrl, 'url' => $url, 'path' => $path, 'params' => $params, 'rPath' => $rPath];

});
```

```
ROUTER::post('/url-to-match',function(&$req,$res,$next){
//send
$res->send('Some Text');
//json
$res->json(['id'=>11,'name'=>'Harpal Singh']);
//redirect using response
$res->redirect('/new-url',$replaceHeaders=false /*(optional)*/, $redirectCode =301  /*(optional)*/);
// Exit
$res->end();
// clear all cookies
$res->clearCookies();
// clear specific cookies
$res->clearCookies();
//set Status
$res->setStatus(404)->send('Not Found');
});
```

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity30

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://avatars.githubusercontent.com/u/83992106?v=4)[Koddn technologies](/maintainers/koddn)[@koddn](https://github.com/koddn)

---

Top Contributors

[![hsk11](https://avatars.githubusercontent.com/u/83590561?v=4)](https://github.com/hsk11 "hsk11 (1 commits)")

---

Tags

express-router-phpfast-routingkoddnnode-router-phpphpphp-api-routerphp-api-routesphp-api-sphp-cms-routerphp-libraryphp-routerphp-router-standalonephp-routingphp7phprouterredirectredirect-urlsrouterrouters

### Embed Badge

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

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

PHPackages © 2026

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