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

1.2.1(3y ago)31981MITPHPPHP &gt;=7.2

Since Aug 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/InitPHP/Router)[ Packagist](https://packagist.org/packages/initphp/router)[ RSS](/packages/initphp-router/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (7)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

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community9

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

Every ~42 days

Recently: every ~53 days

Total

6

Last Release

1155d ago

### 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-routingrouter

###  Code Quality

TestsPHPUnit

### 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

[league/uri-interfaces

Common tools for parsing and resolving RFC3987/RFC3986 URI

536204.9M23](/packages/league-uri-interfaces)[shopify/shopify-api

Shopify API Library for PHP

4634.8M16](/packages/shopify-shopify-api)[laudis/neo4j-php-client

Neo4j-PHP-Client is the most advanced PHP Client for Neo4j

184616.9k31](/packages/laudis-neo4j-php-client)[http-interop/response-sender

A function to convert PSR-7 Response to HTTP output

46711.5k40](/packages/http-interop-response-sender)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[mezzio/mezzio-authentication-oauth2

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

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)

PHPackages © 2026

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