PHPackages                             hypario/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. [Framework](/categories/framework)
4. /
5. hypario/router

ActiveLibrary[Framework](/categories/framework)

hypario/router
==============

A little router for beginners in Object Oriented

1.7.0(5y ago)178[2 PRs](https://github.com/Hypario/Router/pulls)MITPHPPHP &gt;=7.4

Since Dec 20Pushed 3y agoCompare

[ Source](https://github.com/Hypario/Router)[ Packagist](https://packagist.org/packages/hypario/router)[ RSS](/packages/hypario-router/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (4)Versions (22)Used By (0)

A simple Router
===============

[](#a-simple-router)

[![Build Status](https://camo.githubusercontent.com/5bd161fa5c075f6a2c51aa903f84f21c027bf94258014701c933b94d3d5d9200/68747470733a2f2f7472617669732d63692e6f72672f4879706172696f2f526f757465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Hypario/Router)[![Coverage Status](https://camo.githubusercontent.com/1e5b73e4ed4e6abd640f578f0220d910d9a5de347a63f8241fa2a55687ead03e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4879706172696f2f526f757465722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Hypario/Router?branch=master)

This Router is a project for people who want to understand how a router works, and it can be used to start a project really quickly without using any Framework such as Laravel or Symfony

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

[](#installation)

You can install this package using composer

```
composer require hypario/router
```

How to use it
=============

[](#how-to-use-it)

basics
======

[](#basics)

here we will talk about how to use the route for routes that doesn't need parameters

Like every other Router you have to initialize the Router, and write the routes you want like this :

```
$router = new Hypario\Router(); // Here no parameters needed
$router->get('/', function () { echo "Hello World"; }); // Define a route in GET method.
```

There are other method you can choose such as the POST method

```
$router = new Hypario\Router();
$router->post('/', function () { echo "Route accessed via POST method"; });
```

Careful ! those methods don't mean you can reach those pages, now you have to match your URL and the routes

How to match the URL and the route
==================================

[](#how-to-match-the-url-and-the-route)

To match the route you have to use the match method from the router, it will return the route or null if none matched

```
$router = new Hypario\Router();
$router->get('/', function () { echo "Hello World"; });

$route = $router->match($_SERVER['REQUEST_URI']);
```

here `$_SERVER['REQUEST_URI']` is used to get the URL, but you can use an object that implement the ServerRequestInterface from the PSR or a custom $\_GET that give the URL

When you get the matched route, you can get the handler (here the function).

```
$router = new Hypario\Router();
$router->get('/', function () { echo "Hello World"; });

$route = $router->match($_SERVER['REQUEST_URI']); // We get the matched route

if (!is_null($route)) {
    $function = $route->getHandler(); // We get the function
    call_user_func($function); // We call the function of the matched route
}
```

output : `Hello World` if the url is the main page of your website `www.mydomain.com`

The handler can be a string (like the name of a callable class) or a callable (like here, a function) as the router do not handle the way you're calling the handler.

Routes with parameters
======================

[](#routes-with-parameters)

A route with parameters is a classical route, but whenever you want to add parameter, it must be surrounded by `{}` and the name of the parameter and a pattern to match separated by `:` like below.

```
$router->get('/hello/{name:[a-z]+}', function($name) { echo "Hello $name";});

$route = $router->match($_SERVER['REQUEST_URI']);

if (!is_null($route)) {
    $function = $route->getHandler();
    call_user_func_array($function, $route->getParams());
}
```

`{name:[a-z]+}` is one needed parameter of the route `/hello`, which `name` is the name of the parameter, and `[a-z]+` is the pattern to match for the parameter.

Named routes
============

[](#named-routes)

The name of a route is just one more parameter to the method you want to create

```
$router->get('/', 'handler', 'index'); # creates a route called index
```

I can now generate an uri to my index which will return `/`

```
$pathToIndex = $router->getPath('index');
```

It also works for routes with parameters, you only need to add the array of parameters to the function

```
$router->get('/articles/{id:[0-9]+}', 'handler', 'article');

$pathToArticle = $router->getPath('article', ["id" => 1]);
// returns /articles/1
```

If two routes have the same name, it will generate the path of the LAST defined route

```
$router->get('/', 'handler', 'index');
$router->get('/a', 'handler', 'index');

$pathToIndex = $router->getPath('index');
// returns /a
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity70

Established project with proven stability

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

Recently: every ~99 days

Total

19

Last Release

1979d ago

PHP version history (3 changes)1.4.0PHP ^7.1

1.6PHP ^7.4

1.7.0PHP &gt;=7.4

### Community

Maintainers

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

---

Top Contributors

[![Hypario](https://avatars.githubusercontent.com/u/30122807?v=4)](https://github.com/Hypario "Hypario (13 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[symfony/symfony

The Symfony PHP framework

31.3k86.3M2.2k](/packages/symfony-symfony)[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[aimeos/aimeos-core

Full-featured e-commerce components for high performance online shops

4.5k346.9k48](/packages/aimeos-aimeos-core)[silverstripe/framework

The SilverStripe framework

7213.5M2.5k](/packages/silverstripe-framework)[contao/core-bundle

Contao Open Source CMS

1231.6M2.4k](/packages/contao-core-bundle)

PHPackages © 2026

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