PHPackages                             zkrati/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. zkrati/router

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

zkrati/router
=============

PHP router as simple as can be

1.0(7y ago)0411MITPHP

Since Aug 8Pushed 7y agoCompare

[ Source](https://github.com/zkrati/router)[ Packagist](https://packagist.org/packages/zkrati/router)[ RSS](/packages/zkrati-router/feed)WikiDiscussions master Synced today

READMEChangelog (9)DependenciesVersions (15)Used By (0)

zkrati/router
=============

[](#zkratirouter)

PHP router as simple as can be...

- simple and fast yet complex routing
- both static and dynamic routes
- supports GET, POST, PUT, DELETE and PATCH requests

### Version

[](#version)

1.0

### Basic usage

[](#basic-usage)

```
$router = new Zkrati\Routing\SimpleRouter();

$router->get("/test", function() {
    // handle GET request at /test
});

$router->post("/test", function() {
    // handle POST request at /test
});

$router->run();
```

simple right?

You can also use other handler declarations

```
$router->get("/test/path", "Class:methodName");
// this code will create an instance of Class and call it's method methodName

$router->get("/test/path", "Namespace\Class:methodName");
// if you are using namespaces
```

If you need to create your own instance for some reason, you can pass the created instance

```
$instance = new Class();
$router->get("/testuju/path", array($instance, "methodName"));
// this code will use the given instance of Class and call it's method methodName
```

### Variables

[](#variables)

You will propably need to map routes with some varibles in it. It was never been easier.

```
$router->get("/test///path", function($variables) {
    // variables  and  are available in array $variables by it's keys
    // for example with url /test/example/showcase/path

    echo $variables["variable"];      // will output "example"
    echo $variables["next_variable"]; // will output "showcase"
});
```

### Parameters

[](#parameters)

If you need to add the parameters to url, you can. It will be passed to your handler as second parameter.

```
$router->get("/test///path", function($variables, $params) {
    // for url /test/example/showcase/path?optional=true&param=john will passed array $params look like:
    // array(2) {
    //     ["optional"] => string(4) "true"
    //     ["param"] => string(4) "john"
    //   }
});
```

### Url wildcards

[](#url-wildcards)

If you are in situation when you are interested only in first half of the path you can use a wildcard.

```
$router->get("/test/path/*", "Class:methodName");
// this code will match all paths starting /test/path/ for example:
//  - /test/path/first
//  - /test/path/second
//  - /test/path/every/other/path
```

You can even use it as a wildcard for all requests, for example:

```
$router->option("*", "Class:methodName");
// This will handle all option requests
```

You can also combine it with variables:

```
$router->get("/test//*", "Class:methodName");
```

### Headers

[](#headers)

Sometimes is useful to know the request headers. You don't need to search it somewhere anymore. Just add third parameter to your handler function.

```
$router->get("/test///path", function($variables, $params, $headers) {
    // variable $headers is array which contains all request headers
});
```

### Exceptions

[](#exceptions)

It is a good idea to wrap route declarations into try catch because in case of invalid handler it will throw InvalidHandlerException

```
try{

    $router->get("/test/", "Class:firstMethod");
    $router->get("/other/route", "Class:secondMethod");
    $router->get("/cool/route", "invalid handler");

} catch(InvalidHandlerException $e) {
    echo $e->getMessage();
}
```

also $router-&gt;run(); throws RouteNotFoundException when it founds no route for current url

```
try{
    $router->run();
} catch(RouteNotFoundException $e) {
    echo $e->getMessage();
}
```

### Instances

[](#instances)

If you have custom class in your app which manages instances of all other classes and you want tou use string handler definition you can pass your instantiator into router. The router will get class instances from your custom instantiator.

```
$router->setInstantiator($instantiator, "getInstance");
// where $instantiator is your custom instantiator and "getInstance" is name of it´s method to get instance
```

### Todos

[](#todos)

- define variable types and regexp
- add cache support

License
-------

[](#license)

MIT

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 57.1% 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 ~58 days

Total

13

Last Release

2912d ago

Major Versions

0.6.1 → 1.02018-07-10

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/20911818?v=4)[zkrati](/maintainers/zkrati)[@zkrati](https://github.com/zkrati)

---

Top Contributors

[![zkrati](https://avatars.githubusercontent.com/u/20911818?v=4)](https://github.com/zkrati "zkrati (4 commits)")[![janliska](https://avatars.githubusercontent.com/u/4991068?v=4)](https://github.com/janliska "janliska (3 commits)")

### Embed Badge

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

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

###  Alternatives

[butschster/cron-expression-generator

Cron expression generator

511.7M2](/packages/butschster-cron-expression-generator)[sarfraznawaz2005/loading

Laravel package to add loading indicator to pages while page is loading.

4812.0k](/packages/sarfraznawaz2005-loading)[rami/seo-bundle

All in One Symfony Seo Bundle

341.5k](/packages/rami-seo-bundle)

PHPackages © 2026

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