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

ActiveLibrary

webiik/router
=============

The Router is passive, multi-lingual regex router. It supports route names, route parameters, route controllers and route middleware.

1.1(6y ago)0732MITPHPPHP &gt;=7.2

Since Mar 28Pushed 6y ago1 watchersCompare

[ Source](https://github.com/webiik/router)[ Packagist](https://packagist.org/packages/webiik/router)[ Docs](https://www.webiik.com)[ RSS](/packages/webiik-router/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (4)Used By (2)

[![](https://camo.githubusercontent.com/a397347ee4fb199934fee6354504f4702b89f5c22f0ce0ba94c5ff691cde545c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f77656269696b2f77656269696b2e737667)](https://camo.githubusercontent.com/a397347ee4fb199934fee6354504f4702b89f5c22f0ce0ba94c5ff691cde545c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f77656269696b2f77656269696b2e737667)[![](https://camo.githubusercontent.com/20f4b99a958aadb02ff273ac6428c17cf55c6b817657ed64b1c39c7f71955a0e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d302d627269676874677265656e2e737667)](https://camo.githubusercontent.com/20f4b99a958aadb02ff273ac6428c17cf55c6b817657ed64b1c39c7f71955a0e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d302d627269676874677265656e2e737667)

Router
======

[](#router)

The Router is **passive**, multi-lingual regex router. It supports route names, route parameters, route controllers and route middleware. **Passive** means that it doesn't set HTTP headers and it doesn't invoke route controllers and middleware. It just tests a request URI against the defined routes and returns all necessary data to build a route.

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

[](#installation)

```
composer require webiik/router
```

Example
-------

[](#example)

```
$router = new \Webiik\Router\Router();

// Set base URI
$router->setBaseURI(dirname($_SERVER['SCRIPT_NAME']));

// Add route(s)
$router->addRoute(['get'], '/', 'Home:run', 'home-page');

// Check if current URI matches some route
$route = $router->match();

if ($router->getHttpCode() == 200) {
    // 200 - OK
    $route->getLang(); // en
    $route->getName(); // home-page
    $route->getController(); // ['Home', 'run']
} elseif ($router->getHttpCode() == 405) {
    // 405 - Method Not Allowed
} elseif ($router->getHttpCode() == 404) {
    // 404 - Not Found
}
```

Configuration
-------------

[](#configuration)

### setBaseURI

[](#setbaseuri)

```
setBaseURI(string $baseURI): void
```

**setBaseURI()** sets the base directory of your index.php file relatively to web-server root.

```
$router->setBaseURI(dirname($_SERVER['SCRIPT_NAME']));
```

> Every time your index.php file isn't in the web-server root directory, you have to set dir in which is located.

### setDefaultLang

[](#setdefaultlang)

```
setDefaultLang(string $defaultLang): void
```

**setDefaultLang()** sets the default language of routes without defined **$lang** parameter. **$defaultLang** must be two characters long. The default value is **en**.

```
$router->setDefaultLang('en');
```

### setDefaultLangInURI

[](#setdefaultlanginuri)

```
setDefaultLangInURI(bool $defaultLangInURI): void
```

**setDefaultLangInURI()** determines if default language is part of URI e.g. /en/. The default value is **FALSE**.

```
$router->setDefaultLangInURI(true);
```

Adding
------

[](#adding)

### addRoute

[](#addroute)

```
addRoute(array $methods, string $route, string $controller, string $name = '', string $lang = ''): NewRoute
```

**addRoute()** adds **NewRoute** to the **Router** and returns **NewRoute**.

**Parameters:**

- **methods** array of route http methods
- **route** route URI regex (without delimiters)
- **controller** string representation of controller e.g. controllerName:methodName
- **name** route name
- **lang** two letter route lang prefix, if it's not set, the default lang is used instead

```
// Add route
$router->addRoute(['get'], '/', 'Home:run');

// Add route with more http methods
$router->addRoute(['get', 'post'], '/contact', 'Contact:run');

// Add named route
$router->addRoute(['get'], '/', 'Home:run', 'home-page');

// Add named route in specific language
$router->addRoute(['get'], '/', 'Home:run', 'home-page', 'en');

// Add route with route middleware
$router->addRoute(['get'], '/', 'Home:run')->mw('Class:method');

// Add case sensitive route
$router->addRoute(['get'], '/CaMeL', 'Camel:run')->sensitive();

// To add routes with route parameters use regex groups.
// Every regex group represents one route parameter.

// Add route with required parameter
$router->addRoute(['get'], '/portfolio/(?[a-z0-9]+)', 'Portfolio:run');

// Add route with optional parameter
$router->addRoute(['get'], '/portfolio/(?[a-z0-9]+)?', 'Portfolio:run');
```

Check
-----

[](#check)

### match

[](#match)

```
match(): Route
```

**match()** checks if current request URI matches some of defined route and returns **Route**.

```
$route = $router->match();
```

### getHttpCode

[](#gethttpcode)

```
getHttpCode(): int
```

**getHttpCode()** returns http code of the result of last [**match()**](#match).

```
$route = $router->match();
$httpCode = $router->getHttpCode();
if ($httpCode == 200) {
    // 200 OK
} elseif ($httpCode == 405) {
    // 405 Method Not Allowed
} elseif ($httpCode == 404) {
    // 404 Not Found
}
```

Getting
-------

[](#getting)

### getBaseURL

[](#getbaseurl)

```
getBaseURL(): string
```

**getBaseURL()** returns base URL of your app e.g.

```
$baseUrl = $router->getBaseURL();
```

### getURI

[](#geturi)

```
getURI(string $routeName, array $parameters = [], string $lang = ''): string
```

**getURI()** returns route's URI. If it can't find the route or some of the required route parameters is missing, then it returns the empty string. After calling getURI(), you can get missing parameters by calling **[getMissingParameters()](#getmissingparameters)**

```
$route->getURI();
```

### getURL

[](#geturl)

```
getURL(string $routeName, array $parameters = [], string $lang = ''): string
```

**getURL()** same as **[getURI()](#geturi)**, but returns full URL.

```
$route->getURL();
```

### getMissingParameters

[](#getmissingparameters)

```
getMissingParameters(): array
```

**getMissingParameters()** returns missing parameters after calling **getURI()** or **getURL()**.

```
$route->getMissingParameters();
```

### getRegexParameters

[](#getregexparameters)

```
getRegexParameters(string $routeName, string $lang = '')
```

**getRegexParameters()** returns array with route regex parameters e.g. \['0' =&gt; '(?&lt;name&gt;\[a-z\]*)?', '1' =&gt; '(\[a-z\]*)'\]. If the route doesn't exist, it returns false.

```
$route->getRegexParameters();
```

Route
=====

[](#route)

The Route is the result of [**match()**](#match). It contains handy information about the current route.

### getController

[](#getcontroller)

```
getController(): array
```

**getController()** returns array with route controller and controller method to run.

```
$route->getController();
```

### getName

[](#getname)

```
getName(): string
```

**getName()** returns route name.

```
$route->getName();
```

### getLang

[](#getlang)

```
getLang(): string
```

**getLang()** returns route language.

```
$route->getLang();
```

### getMw

[](#getmw)

```
getMw(): array
```

**getMw()** returns array with route middleware.

```
$route->getMw();
```

### getParameters

[](#getparameters)

```
getParameters(): array
```

**getParameters()** returns parameters injected during Route construction e.g. \['name' =&gt; 'dolly', '1' =&gt; 'dolly', '2' =&gt; 'hello'\].

```
$route->getParameters();
```

Resources
---------

[](#resources)

- [Webiik framework](https://github.com/webiik/webiik)
- [Report issue](https://github.com/webiik/components/issues)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity57

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

Total

3

Last Release

2472d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1226362d003d186b45e7dfa44489c36af37196c6a1b476206700eaf4e9c96a5a?d=identicon)[Jiri Mihal](/maintainers/Jiri%20Mihal)

---

Top Contributors

[![Jiri-Mihal](https://avatars.githubusercontent.com/u/10408123?v=4)](https://github.com/Jiri-Mihal "Jiri-Mihal (260 commits)")

---

Tags

routerroutingroute

### Embed Badge

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

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

###  Alternatives

[aura/router

Powerful, flexible web routing for PSR-7 requests.

5231.5M67](/packages/aura-router)[pecee/simple-router

Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router.

696214.6k17](/packages/pecee-simple-router)[pmjones/auto-route

Automatically routes HTTP request to action classes.

20158.6k6](/packages/pmjones-auto-route)[miladrahimi/phprouter

A powerful, lightweight, and very fast HTTP URL router for PHP projects.

20832.6k2](/packages/miladrahimi-phprouter)[izniburak/router

simple router class for php

23522.6k7](/packages/izniburak-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)
