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

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

merophp/router
==============

A request router for PHP

0.1-beta(3y ago)0384↓50%1MITPHPPHP &gt;=7.4

Since Jan 3Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Merophp/router)[ Packagist](https://packagist.org/packages/merophp/router)[ RSS](/packages/merophp-router/feed)WikiDiscussions dev Synced 1mo ago

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

Introduction
============

[](#introduction)

Fast, flexible and extendable request router for PHP.

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

[](#installation)

Via composer:

`composer require merophp/router`Basic Usage
-----------

[](#basic-usage)

Route definitions will be stored in route collectors. The route providers job is to provide routes by a requested HTTP method from its route collection. The router gets the routes from a provider and look for a matching route. He also executes route handlers.

```
require_once 'vendor/autoload.php';

use Merophp\Router\Routes\GetRoute;
use Merophp\Router\Collection\RouteCollection;
use Merophp\Router\Provider\RouteProvider;
use Merophp\Router\Router;

$collection = new RouteCollection;
$collection->addMultiple([
    new GetRoute('/api/v1/foo', function(){
        return 'Hello foo';
    }),
    new GetRoute('/api/v1/bar', function(){
        return 'Hello bar';
    }),
]);

$provider = new RouteProvider($collection);
$router = new Router($provider);

$myRoute = $router->match('GET', '/api/v1/foo');

$result = $router->dispatch($myRoute);
//Will print 'Hello bar'...
echo $result;

```

### Route Definitions

[](#route-definitions)

Route definitions consists of three parts: the HTTP methods, a pattern and a handler.

```
use Merophp\Router\Routes\Route;

new Route(['GET','POST'], '/api/v1/foo', function(){
    echo 'I will be executed';
});

```

One route definition for all HTTP methods is also possible with a wildcard:

```
new Route(['*'], '/api/v1/foo', function(){
    echo 'I am the handler';
});

```

For the case that a route definition has only one HTTP method, you can also use following classes:

- Merophp\\Router\\Routes\\GetRoute
- Merophp\\Router\\Routes\\PostRoute
- Merophp\\Router\\Routes\\PutRoute
- Merophp\\Router\\Routes\\DeleteRoute
- Merophp\\Router\\Routes\\PatchRoute
- Merophp\\Router\\Routes\\OptionsRoute
- Merophp\\Router\\Routes\\HeadRoute

```
new GetRoute('/api/v1/foo', function(){
    echo 'I am the handler for GET:/api/v1/foo';
});
new PostRoute('/api/v1/foo', function(){
    echo 'I am the handler for POST:/api/v1/foo';
});

```

#### The Pattern

[](#the-pattern)

A routes pattern will be compared to a given URI path. The dispatcher will find the best fitting route definition for a given HTTP method and a URI path. You can use wildcards and placeholders in your route pattern. Values for placeholders will be passed to the routes handler.

```
new GetRoute('*', function(){
    echo 'I will be executed if no other definition fits';
});
new GetRoute('/api/v1/*', function(){

});
new GetRoute('/api/v1/events/{eventid}', function($eventid){
    echo $eventid;
});
new DeleteRoute('/api/v1/events/{eventid}', function($eventid){
    echo $eventid.' deleted';
});

```

#### The Handler

[](#the-handler)

Any callable can be used as a route handler.

#### Scopes

[](#scopes)

Scopes are brackets for routes and made to prevent you from writing the same pattern parts for different routes multiple times.

```
use Merophp\Router\Routes\Scope;
use Merophp\Router\Routes\GetRoute;
use Merophp\Router\Routes\DeleteRoute;

new Scope('/api/v1/events/{eventid}', [
    new GetRoute('', function(){}),
    new DeleteRoute('', function(){})
]);

```

The pattern of the scope will be used as prefix for the patterns of the routes.

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

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

Total

2

Last Release

1378d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3f873869f98cb1bdf9edb5847f987dfd216e5bc44f934d87875dca5ee20fbefb?d=identicon)[Robert.Becker](/maintainers/Robert.Becker)

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M319](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M293](/packages/pusher-pusher-php-server)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78026.4M414](/packages/react-http)[php-http/curl-client

PSR-18 and HTTPlug Async client with cURL

48347.0M384](/packages/php-http-curl-client)[smi2/phpclickhouse

PHP ClickHouse Client

84310.1M71](/packages/smi2-phpclickhouse)

PHPackages © 2026

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