PHPackages                             initphp/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. [HTTP &amp; Networking](/categories/http)
4. /
5. initphp/router

ActiveLibrary[HTTP &amp; Networking](/categories/http)

initphp/router
==============

InitPHP HTTP Router Library

2.1.0(1w ago)32031MITPHPPHP &gt;=8.1

Since Aug 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/InitPHP/Router)[ Packagist](https://packagist.org/packages/initphp/router)[ Docs](https://github.com/InitPHP/Router)[ GitHub Sponsors](https://github.com/muhammetsafak)[ RSS](/packages/initphp-router/feed)WikiDiscussions main Synced today

READMEChangelog (3)Dependencies (9)Versions (10)Used By (1)

InitPHP Router
==============

[](#initphp-router)

This is an open source library that lets you create and manage advanced routes for HTTP requests.

[![Latest Stable Version](https://camo.githubusercontent.com/49a8d82646402e97ccedbc58f395d3acbee1ae1d78e733c5a79f13259a658f49/687474703a2f2f706f7365722e707567782e6f72672f696e69747068702f726f757465722f76)](https://packagist.org/packages/initphp/router) [![Total Downloads](https://camo.githubusercontent.com/13a967b55a714776163d35aaa16f7030531c96f5b63045627dec32b40aef53bd/687474703a2f2f706f7365722e707567782e6f72672f696e69747068702f726f757465722f646f776e6c6f616473)](https://packagist.org/packages/initphp/router) [![Latest Unstable Version](https://camo.githubusercontent.com/2d54c16bbac41206211c50d1ef97e086583a4ed75e6575cfadaf3f232c8e9b3a/687474703a2f2f706f7365722e707567782e6f72672f696e69747068702f726f757465722f762f756e737461626c65)](https://packagist.org/packages/initphp/router) [![License](https://camo.githubusercontent.com/6ac5a5b653ee4ef3476ac042fe168329753830afb287b04ba401aab70d8709bd/687474703a2f2f706f7365722e707567782e6f72672f696e69747068702f726f757465722f6c6963656e7365)](https://packagist.org/packages/initphp/router) [![PHP Version Require](https://camo.githubusercontent.com/9870f91d3a38a9526774f42ef6bf6949ca9dd4fe4c0122f4d1e091feaafac36d/687474703a2f2f706f7365722e707567782e6f72672f696e69747068702f726f757465722f726571756972652f706870)](https://packagist.org/packages/initphp/router)

Features
--------

[](#features)

- Full support for GET, POST, PUT, DELETE, OPTIONS, PATCH, HEAD and ANY request methods.
- Variable request methods with (Laravel-like) `$_REQUEST['_method']`. (Default is off, optionally can be activated)
- Controller support. (HomeController@about or HomeController::about)
- Middleware/Filter (before and after) support.
- Static and dynamic route patterns.
- Ability to create custom parameter pattern.
- Namespace support.
- Route grouping support.
- Domain-based routing support.
- Ability to define custom 404 errors
- Ability to name routes
- Ability to call a class in (Symfony-like) callable functions or parameters of controller methods.
- Routing by request ports.
- Routing by client IP address (Via `$_SERVER['REMOTE_ADDR']`. Locally, this value can be something like `::1` or `127.0.0.1`.)
- A directory path can be defined as a virtual link.

Requirements
------------

[](#requirements)

- PHP 7.2 or later
- Apache is; **AllowOverride All** should be set to and **mod\_rewrite** should be on.
- Any library that implements the [Psr-7 HTTP Message Interface](https://www.php-fig.org/psr/psr-7/) and an emitter written for Psr-7. For example; [InitPHP HTTP](https://github.com/InitPHP/HTTP) Library

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

[](#installation)

```
composer require initphp/router

```

Is Apache `.htaccess`

```
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

```

Is NGINX;

```
server {
	listen 80;
	server_name myinitphpdomain.dev;
	root /var/www/myInitPHPDomain/public;
	index index.php;
	location / {
		try_files $uri $uri/ /index.php?$query_string;
	}
	location ~ \.php$ {
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass unix:/var/run/php7.4-fpm.sock;
		fastcgi_index index.php;
		include fastcgi.conf;
		fastcgi_intercept_errors on;
	}
}

```

Configuration
-------------

[](#configuration)

```
$config = [
    'paths'                 => [
        'controller'            => null, //The full path to the directory where the Controller classes are kept.
        'middleware'            => null, //The full path to the directory where the Middleware classes are kept.
    ],
    'namespaces'            => [
        'controller'            => null, //Namespace prefix of Controller classes, if applicable.
        'middleware'            => null, //Namespace prefix of Middleware classes, if applicable.
    ],
    'base_path'             => '/', // If you are working in a subdirectory; identifies your working directory.
    'variable_method'       => false, // It makes the request method mutable with Laravel-like $_REQUEST['_method'].
    'argument_new_instance' => false, // This configuration is used for Request and Response objects that you want as arguments.
];
```

Usage
-----

[](#usage)

The following example uses the [InitPHP HTTP](https://github.com/InitPHP/HTTP) library. If you wish, you can use this library using the command below, or you can perform similar operations using another library that uses the Psr-7 HTTP Message interface.

```
composer require initphp/http

```

***See the Wiki for detailed documentation.***

```
require_once "vendor/autoload.php";
use \InitPHP\HTTP\Message\{Request, Response, Stream};
use \InitPHP\HTTP\Emitter\Emitter;
use \InitPHP\Router\Router;

$request = Request::createFromGlobals();
$response = new Response();

// Create the router object.
$router = new Router($request, $response, []);

// ... Create routes.
$router->get('/', function () {
    return 'Hello World!';
});

$router->post('/login', function (Request $request, Response $response) {
    return $response->json([
        'status'        => 0,
        'message'       => 'Unauthorized',
    ], 401);
});

// If you do not make a definition for 404 errors; An exception is thrown if there is no match with the request.
$router->error_404(function () {
    echo 'Page Not Found';
});

// Resolve the current route and get the generated HTTP response object.
$response = $router->dispatch();

// Publish the HTTP response object.
$emitter = new Emitter;
$emitter->emit($response);
```

Getting Help
------------

[](#getting-help)

If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.

Getting Involved
----------------

[](#getting-involved)

> All contributions to this project will be published under the MIT License. By submitting a pull request or filing a bug, issue, or feature request, you are agreeing to comply with this waiver of copyright interest.

There are two primary ways to help:

- Using the issue tracker, and
- Changing the code-base.

### Using the issue tracker

[](#using-the-issue-tracker)

Use the issue tracker to suggest feature requests, report bugs, and ask questions. This is also a great way to connect with the developers of the project as well as others who are interested in this solution.

Use the issue tracker to find ways to contribute. Find a bug or a feature, mention in the issue that you will take on that effort, then follow the Changing the code-base guidance below.

### Changing the code-base

[](#changing-the-code-base)

Generally speaking, you should fork this repository, make changes in your own fork, and then submit a pull request. All new code should have associated unit tests that validate implemented features and the presence or lack of defects. Additionally, the code should follow any stylistic and architectural guidelines prescribed by the project. In the absence of such guidelines, mimic the styles and patterns in the existing code-base.

Credits
-------

[](#credits)

- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) &lt;&gt;

License
-------

[](#license)

Copyright © 2022 [MIT Licence](./LICENSE)

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance54

Moderate activity, may be stable

Popularity16

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

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

Recently: every ~297 days

Total

9

Last Release

13d ago

Major Versions

1.x-dev → 2.0.02026-06-09

PHP version history (2 changes)1.1.3PHP &gt;=7.2

2.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/4b6b34f3ac8938d8ee52ba3bd260680855dc5715c7b2929d9380de30d15a67dd?d=identicon)[muhammetsafak](/maintainers/muhammetsafak)

---

Top Contributors

[![muhammetsafak](https://avatars.githubusercontent.com/u/104234499?v=4)](https://github.com/muhammetsafak "muhammetsafak (16 commits)")

---

Tags

phpphp-routephp-routerphp-routingrouterhttppsr-7middlewarePSR-11routerroutinginitphp

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.1B4.0k](/packages/guzzlehttp-psr7)[mezzio/mezzio

PSR-15 Middleware Microframework

3923.8M126](/packages/mezzio-mezzio)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[sunrise/http-router

A powerful solution as the foundation of your project.

17451.6k10](/packages/sunrise-http-router)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28591.3k3](/packages/mezzio-mezzio-authentication-oauth2)[middlewares/utils

Common utils for PSR-15 middleware packages

503.7M94](/packages/middlewares-utils)

PHPackages © 2026

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