PHPackages                             memcrab/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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. memcrab/router

ActivePackage[Parsing &amp; Serialization](/categories/parsing)

memcrab/router
==============

PHP Router based on memCrab Core API

1.4.3(4y ago)350.1k↓36%2MITPHPPHP &gt;=7.1CI failing

Since Jan 30Pushed 4y ago3 watchersCompare

[ Source](https://github.com/Memcrab/Router)[ Packagist](https://packagist.org/packages/memcrab/router)[ Docs](http://memcrab.com)[ RSS](/packages/memcrab-router/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (2)Versions (24)Used By (0)

PHP Router as Composer Library
==============================

[](#php-router-as-composer-library)

### Status

[](#status)

[![Build Status](https://camo.githubusercontent.com/2ec4b6f9f4419a53f377272bfb5b97731a18c09dbb5556d23d5608b78353a94d/68747470733a2f2f7472617669732d63692e6f72672f6e6f6f6e65686f732f726f757465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/noonehos/router)[![Dependency Status](https://camo.githubusercontent.com/77f479410fa378d99b8280888020f97c3f76b9957861c31046c7fd386d00f348/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3538386639306331373630636536303033613465613637362f62616467652e7376673f7374796c653d666c61742d737175617265)](https://www.versioneye.com/user/projects/588f90c1760ce6003a4ea676)[![Total Downloads](https://camo.githubusercontent.com/afccf1a434b954b36d36340285677a5d622afbf9b000296a725588220d3be66f/68747470733a2f2f706f7365722e707567782e6f72672f6d656d637261622f726f757465722f646f776e6c6f616473)](https://packagist.org/packages/memcrab/router)[![Latest Stable Version](https://camo.githubusercontent.com/eda6e1468886357018eeaa3e5b587718f745d8a7198d6a632609c68424d4596e/68747470733a2f2f706f7365722e707567782e6f72672f6d656d637261622f726f757465722f76657273696f6e)](https://packagist.org/packages/memcrab/router)[![Latest Unstable Version](https://camo.githubusercontent.com/8f0a0e761039b6a063d953b9acc5480907e69d6f92a6061c44406315e98871e0/68747470733a2f2f706f7365722e707567782e6f72672f6d656d637261622f726f757465722f762f756e737461626c65)](//packagist.org/packages/memcrab/router)[![License](https://camo.githubusercontent.com/1ee2de89a3d0db419f421aee3a5554bf2881c303868e530a70484248b2bc1b7c/68747470733a2f2f706f7365722e707567782e6f72672f6d656d637261622f726f757465722f6c6963656e7365)](https://packagist.org/packages/memcrab/router)[![composer.lock available](https://camo.githubusercontent.com/6bf2d3c0c9738c711c9e2845c79ee57ed6fba7f1fa0afd95e452b16fdbe58b25/68747470733a2f2f706f7365722e707567782e6f72672f6d656d637261622f726f757465722f636f6d706f7365726c6f636b)](https://packagist.org/packages/memcrab/router)

It's php router based on yaml configuration file and support regular expressions in each route condition. Thats help build more accurate routes with only numbers in part of url or with required part of word etc.

Features
--------

[](#features)

- Support RegExp in any kind of route
- Support multiple routings for single url throw different request methods (POST, GET, PUT, DELETE, ...)
- Support full url or just request uri
- All configurations in simple YAML file
- Each route can return already named params (as many params as you want, or as you have in Regular Expression)
- High performance yaml parse throw using updated pecl yaml-ext 2.0.0 for php 7.0
- Strict standart coding with full Typing of params and returns (by php 7.1)
- PSR-4 autoloading compliant structure
- Unit-Testing with PHPUnit
- Easy to use to any framework

Install
-------

[](#install)

`composer require memcrab/router`

Dependencies
------------

[](#dependencies)

php extension YAML:

- for Ubuntu/Debian

```
- apt-get update
- apt-get install php-pear
- apt-get install php-dev
- apt-get install php-xml php7.0-xml
- apt-get install libyaml-dev
- pecl channel-update pecl.php.net
- pecl install yaml-2.0.0

```

- for OS X

```
- brew install php71 --with-pear
- brew install autoconf
- touch $(brew --prefix php71)/lib/php/.lock && chmod 0644 $(brew --prefix php71)/lib/php/.lock
- pecl install yaml-2.0.0

```

Usage
-----

[](#usage)

- init Router: `memCrab\Router()`
- load routes: `->loadRoutesFromYaml(string $filePath)`
    - $filePath - Path to yaml files with routes
- run matching: `->matchRoute(string $url, string $method)`
    - $url - URL (`http://example.com/posts`) or just request URI of page (`/post`)
    - $method - http request method
- use your router data with:
    - getService() - return component that we call
    - getAction() - return action that will be run from component
    - getParams() - return route regExp params

Yaml Config Example
-------------------

[](#yaml-config-example)

```
/:
  GET: [Index, getMain]
/post/:
  GET:    [Post, get]
  POST:   [Post, add]
  PATCH:  [Post, save]
  DELETE: [Post, delete]
/post/publish/:
  POST: [Post, setPublishing]
/catalog/([a-zA-Z0-9]+)-([a-zA-Z0-9]+)/:
  GET: [Catalog, filter, key1, value1]
```

Run Example
-----------

[](#run-example)

```
