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

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

mateodioev/http-router
======================

Simple route in php with support for vars in url

v1.2.2(2y ago)242MITPHP

Since Jun 16Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Mateodioev/simpleroute)[ Packagist](https://packagist.org/packages/mateodioev/http-router)[ RSS](/packages/mateodioev-http-router/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (7)Used By (0)

Simple HTTP router
==================

[](#simple-http-router)

> ⚠️ **This project was made for educational purposes only.** It is not intended to be used in production environments.

Usage
-----

[](#usage)

```
use Mateodioev\HttpRouter\exceptions\{HttpNotFoundException, RequestException};
use Mateodioev\HttpRouter\{Request, Response, Router};

$router = new Router();

// Register your endpoints here
$router->get('/', function (Request $r) {
    return Response::text('Hello world!');
});

$router->run();
```

### Methods

[](#methods)

You can use the `Router::get`, `Router::post`, `Router::put`, `Router::patch` and `Router::delete` methods to register your endpoints.

```
$router->myHttpMethod($uri, $callback);
```

- `$uri` is the path of the endpoint.
- `$callback` is the function that will be executed when the endpoint is requested. Each callback (action) must return an instance of the Mateodioev\\HttpRouter\\Response class or an InvalidReturnException will be thrown.

#### Static files

[](#static-files)

You can map all static files in a directory with the `static` method.

```
$router->static($baseUri, $path, $methods);
// Default methods are GET
```

Example:

```
tree
```

```
.
├── index.php
└── styles.css

1 directory, 2 files

```

```
$router->static('/docs', 'public/');
```

Now you can reach this uris

- /docs/index.php
- /docs/styles.css

#### Handling all HTTP methods

[](#handling-all-http-methods)

You can use the `Router::all` method to handle all HTTP methods with one callback.

```
$router->all($uri, $callback);
```

### Path parameters

[](#path-parameters)

You can use path parameters in your endpoints. Path parameters are defined by a bracket followed by the name of the parameter.

> `/users/{id}`

```
$router->get('/page/{name}', function (Request $r) {
    return Response::text('Welcome to ' . $r->param('name'));
});
```

**Note:** You can make a parameter optional by adding a question mark after the name of the parameter.

> `/users/{id}?`

```
$router->get('/page/{name}?', function (Request $r) {
    $pageName = $r->param('name') ?? 'home'; // If the parameter is not present, the method return null
    return Response::text('Welcome to ' . $pageName);
});
```

### Request data

[](#request-data)

You can get all data from the request with the following methods:

- `Request::method()` returns the HTTP method of the request.
- `Request::uri()` returns the URI of the request.
- `Request::uri()` returns the URL of the request.
- `Request::param($name, $default = null)` returns the value of the parameter with the name `$name` or null if the parameter is not present.
- `Request::params()` return al the request URI parameters.
- `Request::headers()` returns an array with all the headers of the request.
- `Request::body()` returns the body of the request (from [php://input](https://www.php.net/manual/en/wrappers.php.php)).
- `Request::data()` returns an array with all the data of the request. Use this when Content-Type is *application/x-www-form-urlencoded* or *multipart/form-data*.
- `Request::files()` returns an array with all the files of the request.
- `Request::query()` returns an array with all the query parameters from the uri.

*TODO list:*

- Add support for middlewares.
- Add support for custom error handlers.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

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

Total

6

Last Release

1050d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/77e3f2a5c4e98caf6e911141af3d18235c658500e8b9e0a95ec251a803a39676?d=identicon)[Mateodioev](/maintainers/Mateodioev)

---

Top Contributors

[![Mateodioev](https://avatars.githubusercontent.com/u/68271130?v=4)](https://github.com/Mateodioev "Mateodioev (12 commits)")

### Embed Badge

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

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

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.0B3.1k](/packages/guzzlehttp-psr7)[friendsofsymfony/rest-bundle

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

2.8k73.3M317](/packages/friendsofsymfony-rest-bundle)[react/http

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

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

PSR-18 and HTTPlug Async client with cURL

48247.0M383](/packages/php-http-curl-client)[smi2/phpclickhouse

PHP ClickHouse Client

83510.1M71](/packages/smi2-phpclickhouse)[paragonie/csp-builder

Easily add and update Content-Security-Policy headers for your project

5412.8M18](/packages/paragonie-csp-builder)

PHPackages © 2026

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