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(1mo ago)32031MITPHPPHP &gt;=8.1CI passing

Since Aug 20Pushed 1mo 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 1w ago

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

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

[](#initphp-router)

A fast, framework-agnostic HTTP router for PHP. Define expressive routes for any HTTP method, group them by prefix, domain, port or client IP, attach middleware, inject dependencies into your handlers, generate URLs from named routes and serve static files — all on top of any [PSR-7](https://www.php-fig.org/psr/psr-7/)request/response implementation.

[![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) [![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)

- GET, POST, PUT, DELETE, OPTIONS, PATCH, HEAD and ANY methods (plus virtual `LINK` routes).
- Optional Laravel-style method override via `$_REQUEST['_method']`.
- Controller handlers (`Home@about`, `Home::about`, `Home->about`, or `[Home::class, 'about']`).
- Before/after middleware (filters), per route and per controller.
- Static and dynamic route patterns, with a customizable pattern registry.
- Route grouping by prefix, domain, port and client IP.
- Named routes and URL generation.
- Reflection-based dependency injection for handlers, with optional [PSR-11](https://www.php-fig.org/psr/psr-11/) container support.
- Customizable 404 handling.
- Serve files or whole directories as virtual links (with path-traversal protection).
- Optional route caching.
- First-class liveness/readiness health endpoints and a production-readiness linter.

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

[](#requirements)

- PHP 8.1 or later
- Any [PSR-7](https://www.php-fig.org/psr/psr-7/) HTTP message implementation and a PSR-7 emitter. The examples below use [InitPHP HTTP](https://github.com/InitPHP/HTTP).
- For pretty URLs, a front controller (`index.php`) with URL rewriting (see below).

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

[](#installation)

```
composer require initphp/router
```

The examples in the documentation use the InitPHP HTTP library:

```
composer require initphp/http
```

### Web server

[](#web-server)

Apache (`.htaccess`):

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

NGINX:

```
location / {
    try_files $uri $uri/ /index.php?$query_string;
}
```

Quick start
-----------

[](#quick-start)

```
