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

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

denniscarrazeiro/php-router-module
==================================

A library that proposes handle http routes module. Consider that Module goes be use in PHP projects.

1.0.0(11mo ago)00MITPHPPHP &gt;=7.2

Since May 31Pushed 11mo agoCompare

[ Source](https://github.com/denniscarrazeiro/php-router-module)[ Packagist](https://packagist.org/packages/denniscarrazeiro/php-router-module)[ Docs](https://github.com/denniscarrazeiro/)[ RSS](/packages/denniscarrazeiro-php-router-module/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

PHP Router Module
=================

[](#php-router-module)

[![Maintainer](https://camo.githubusercontent.com/efc6f9a96627b72a38ee841220af7aa2329a212f469d3174d45579001cd6781f/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6d61696e7461696e65722d64656e6e697363617272617a6569726f2d626c75652e7376673f7374796c653d666c61742d737175617265)](https://www.linkedin.com/in/dennis-carrazeiro)[![PHP from Packagist](https://camo.githubusercontent.com/d0049d210d83e86776e4238d78ac6f8ce9cd09255bea404a99c2fce6fc2dd50d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f64656e6e697363617272617a6569726f2f7068702d726f757465722d6d6f64756c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/denniscarrazeiro/php-router-module)[![Latest Version](https://camo.githubusercontent.com/8211c87fdda55d49df3c3057b82208284a5cb90f35923df5696a5fcd63791793/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f64656e6e697363617272617a6569726f2f7068702d726f757465722d6d6f64756c652e7376673f7374796c653d666c61742d737175617265)](https://github.com/denniscarrazeiro/php-router-module/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

This PHP Router class provides a robust and easy-to-use routing solution for your web applications, offering many features to ensure a single, consistent routing instance throughout your project. It enables you to define routes for various HTTP methods (GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD), incorporating support for middleware to execute logic before your main route callbacks. The router dynamically handles URL parameters, provides static access to current request details (URI, method, and parameters), and includes built-in mechanisms for handling 404 Not Found and 405 Method Not Allowed errors, making it a comprehensive tool for managing your application's request flow.

Instalation
-----------

[](#instalation)

```
bash ./scripts/composer_install.sh
```

Composer is a dependency manager for the PHP programming language. Therefore, after running the command above, Composer will install all the necessary dependencies to ensure the project functions under the best possible conditions.

Unit Tests
----------

[](#unit-tests)

```
bash ./scripts/phpunit_tests.sh
```

PHPUnit is a programmer-oriented testing framework for PHP, designed to facilitate the creation and execution of unit tests. Consequently, after setting up your test suite and running the appropriate command, PHPUnit will execute your tests and provide detailed feedback, ensuring your codebase maintains a high level of quality and reliability.

PHP Built-in Web Server
-----------------------

[](#php-built-in-web-server)

```
bash ./scripts/start_php_server.sh
```

The PHP built-in web server is a command-line tool that provides a quick and convenient way to test PHP applications locally. Consequently, after navigating to your project's root directory and running the appropriate command, the PHP built-in web server will serve your files over HTTP, allowing you to access and test your application directly in a web browser without the need for a full-fledged web server like Apache or Nginx.

Bruno
-----

[](#bruno)

> You can import Bruno collection located in **'http'** folder to test the requests.

Bruno is an open-source, Git-friendly, and offline-first API client designed for exploring and testing APIs. Consequently, after setting up your API collections as plain text files within your project's version control system, Bruno will allow you to send requests, analyze responses, and manage your API environments, ensuring seamless collaboration and a streamlined workflow for API development and testing.

Server configuration before use
-------------------------------

[](#server-configuration-before-use)

> Remember: You can use the built-in web server run the command start\_php\_server.sh to execute the example/index.php

### Apache

[](#apache)

The .htaccess f\*ile is a powerful, directory-level configuration file used on Apache web servers. For your PHP Router class, it plays a crucial role by enabling URL rewriting, which is essential for a clean and efficient routing system. **Enable mod\_rewrite in your apache server and add the following .htaccess in the root of your php project**:

```

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?__ROUTER__=/$1 [L,QSA]

```

### Nginx

[](#nginx)

Nginx uses its own configuration syntax for URL rewriting. You typically configure this in your server's nginx.conf file or a site-specific configuration file within sites-available (and symlinked to sites-enabled). For your Router class, an Nginx configuration would look something like this within your server block:

```
server {
    listen 80;
    server_name yourdomain.com;
    root /path/to/your/project/public; # Assuming index.php is in a 'public' directory

    index index.php;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.x-fpm.sock; # Adjust for your PHP-FPM version
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING __ROUTER__=$uri&$query_string; # Pass __ROUTER__ as query param
    }
}

```

Examples
--------

[](#examples)

### Usage Example 1

[](#usage-example-1)

```
require_once __DIR__ . "/vendor/autoload.php";

use DennisCarrazeiro\Php\Router\Module\Router\Router;
use DennisCarrazeiro\Php\Router\Module\Router\RouterDTO;

$router = new Router();
$router->get(
    RouterDTO::route('/home')
        ->callback(function(){
            echo "Hello World!";
        })
        ->middleware(function (){
            echo "Hello I am middleware!"
        })
        ->name('home')
);
$router->dispatch();
```

### Usage Example 2

[](#usage-example-2)

```
require_once __DIR__ . "/vendor/autoload.php";

use DennisCarrazeiro\Php\Router\Module\Router\Router;
use DennisCarrazeiro\Php\Router\Module\Router\RouterDTO;

$router = new Router();
$router->post(
    RouterDTO::route('/user/{id}')
        ->callback(function($id){
            echo "User profile updated!";
        })
        ->middleware(function(){
            if(!isset($_POST['csrf']) || $_POST['csrf'] !== '123456789'){
                throw new \Exception("Csrf is required.");
            }
        })
        ->name('user.profile')
);
$router->dispatch();
```

Full example of use
-------------------

[](#full-example-of-use)

For more examples see the [Examples](https://github.com/denniscarrazeiro/php-router-module/blob/master/example) folder.

License
-------

[](#license)

The MIT license. Please see [License file](https://github.com/denniscarrazeiro/php-router-module/blob/master/LICENSE) for more information.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance50

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity31

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

352d ago

### Community

Maintainers

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

---

Top Contributors

[![denniscarrazeiro](https://avatars.githubusercontent.com/u/5447197?v=4)](https://github.com/denniscarrazeiro "denniscarrazeiro (3 commits)")

---

Tags

routesrouterCarrazeiro

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[symfony/routing

Maps an HTTP request to a set of configuration variables

7.6k789.4M1.8k](/packages/symfony-routing)[nikic/fast-route

Fast request router for PHP

5.3k92.4M668](/packages/nikic-fast-route)[league/route

Fast routing and dispatch component including PSR-15 middleware, built on top of FastRoute.

6633.1M115](/packages/league-route)[io-developer/php-whois

PHP WHOIS provides parsed and raw whois lookup of domains and ASN routes. PHP 5.4+ and 7+ compatible

4922.4M8](/packages/io-developer-php-whois)[coffeecode/router

A classic CoffeeCode Router is easy, fast and extremely uncomplicated. Create and manage your routes in minutes!

181111.1k5](/packages/coffeecode-router)[contributte/api-router

RESTful Router for your Apis in Nette Framework - created either directly or via attributes

20802.8k3](/packages/contributte-api-router)

PHPackages © 2026

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