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

ActiveLibrary

kambo/router
============

Kambo router

v0.9(8y ago)011MITPHPPHP &gt;=7.0

Since Jul 6Pushed 8y ago1 watchersCompare

[ Source](https://github.com/kambo-1st/KamboRouter)[ Packagist](https://packagist.org/packages/kambo/router)[ RSS](/packages/kambo-router/feed)WikiDiscussions master Synced 2mo ago

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

Kambo PHP router
================

[](#kambo-php-router)

[![Build Status](https://camo.githubusercontent.com/94c654ee7af2ab4ba89f982338ad3aba6222785977342dcc2102f91c54d6c1f0/68747470733a2f2f7472617669732d63692e6f72672f6b616d626f2d3173742f4b616d626f526f757465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/kambo-1st/KamboRouter)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/8651934bf407e603658277b1eee784d1e0623a0ff2086ce551526589444d7950/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b616d626f2d3173742f4b616d626f526f757465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/kambo-1st/KamboRouter/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/b3c840adf0f9a6d75df43c4e608c12bacf1b6253b5f4475c8bfe4be91511d5a5/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6b616d626f2d3173742f4b616d626f526f757465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/kambo-1st/KamboRouter/)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

Just another PHP router with following highlights:

- Support of PSR-7 - HTTP message interfaces
- Two dispatchers with closure and controller/module support
- Can be used even without mod\_rewrite

Install
-------

[](#install)

Prefered way to install library is with composer:

```
composer require kambo/router
```

Usage
-----

[](#usage)

### URL rewrite

[](#url-rewrite)

For rewrite support in Apache with enabled mod\_rewrite create a .htaccess file in your root directory, with following settings:

```
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php [NC,L]
```

For disabling support of mod\_rewrite use method setUrlFormat:

```
$matcher->setUrlFormat(RouteMode::GET_FORMAT);
```

### Route definition

[](#route-definition)

The routes are added by calling `createRoute()` on the RouteCollection instance:

```
$routeCollection->createRoute($method, $routePattern, $handler);
```

The `$method` is HTTP method name represented by value from Kambo\\Router\\Enum\\Method enum for which a certain route should match, eg.: Method::GET

By default the `$routePattern` uses a syntax where `{foo}` specifies a placeholder with name `foo`and matching the regex `[^/]+`. To adjust the pattern the placeholder matches, you can specify a custom pattern by writing `{bar:[0-9]+}`. Some examples:

```
// Matches /user/kambo/123, but not /user/kambo/abc
$routeCollection->addRoute(Method::GET, '/user/{name}/{id:\d+}', $handler);
```

A shortcut methods can be also used for all Method:

```
// Shortcut for createRoute(Method::GET, '/user/{name}/{id:\d+}', $handler);

$routeCollection->get('/user/{name}/{id:\d+}', $handler)
$routeCollection->post('post/url', $handler)
$routeCollection->delete('delete/url', $handler)
$routeCollection->put('put/url', $handler)
$routeCollection->any('any/url', $handler)
```

A closure as `$handler` can be used:

```
$routeCollection->get('/article/{id:\d+}', function($id) {
    echo $id;
});
```

### PSR-7 - HTTP message interfaces

[](#psr-7---http-message-interfaces)

Kambo router is using a instance of PSR 7 compatible request object for abstraction over server variables. Any third party library that implements PSR-7 can be used, such as [Kambo/HttpMessage](https://github.com/kambo-1st/HttpMessage)

### Router dispatcher

[](#router-dispatcher)

Router comes with following dispatcher:

- Closure dispatcher with automatic path &lt;=&gt; closure variable bind function.
- Opinionated class dispatcher which force you organize your code into module/controller class structure.

#### Using closure dispatcher

[](#using-closure-dispatcher)

```
