2017 © Pedro Peláez
 

windwalker-package router

Windwalker Router package

image

windwalker/router

Windwalker Router package

  • Sunday, July 8, 2018
  • by asika32764
  • Repository
  • 3 Watchers
  • 0 Stars
  • 3,280 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 52 Versions
  • 1 % Grown

The README.md

Windwalker Router

Installation via Composer

Add this to the require block in your composer.json., (*1)

``` json { "require": { "windwalker/router": "~3.0" } }, (*2)


## Getting Started ``` php use Windwalker\Router\Router; $router = new Router;

Add Routes

``` php use Windwalker\Route\Route;, (*3)

// Route with name $router->addRoute(new Route('sakura', 'flower/(id)/sakura', array('_controller' => 'SakuraController')));, (*4)

// Route without name $router->addRoute(new Route(null, 'flower/(id)/sakura', array('_controller' => 'SakuraController')));, (*5)


### Match Route ``` php $route = $router->match('flower/12/sakura'); $variables = $route->getVariables(); // Array([_controller] => SakuraController) // Use variables $class = $variables['_controller']; $controller = new $class;

Add More Options to Route

Route interface: '($name, $pattern[, $variables = array()][, $allowMethods = array()][, $options = array()])', (*6)

``` php $route = new Route( 'name', 'pattern/of/route/(id).(format)',, (*7)

// Default Variables
array(
    'id'    => 1,
    'alias' => 'foo-bar-baz',
    'format' => 'html'
),

// Allow methods
array('GET', 'POST'),

// Options
array(
    'host'    => 'windwalker.io',
    'scheme'  => 'http', // Only http & https
    'port'    => 80,
    'sslPort' => 443,
    'requirements' => array(
        'id' => '\d+'
    ),
    'extra' => array(
        '_ctrl' => 'Controller\Class\Name',
    )
)

);, (*8)

$router->addRoute($route);, (*9)

// match routes $route = $router->match( 'pattern/of/route/25.html', array( 'host' => $uri->getHost(), 'scheme' => $uri->getScheme(), 'port' => $uri->getPort() ) );, (*10)

$variables = $route->getVariables();, (*11)

// Merge these matched variables back to http request $_REQUEST = array_merge($_REQUEST, $variables);, (*12)

// Extra is the optional variables but we won't want to merge into request $extra = $router->getExtra();, (*13)

print_r($variables); print_r($extra);, (*14)


The printed result:

Array ( [id] => 25 [alias] => foo-bar-baz [format] => html ), (*15)

Array( [_ctrl] => Controller\Class\Name ), (*16)


### Build Route `build()` is a method to generate route uri for view template. ``` php $router->addRoute(new Route('sakura', 'flower/(id)/sakura', array('_controller' => 'SakuraController'))); $uri = $router->build('sakura', array('id' => 30)); // flower/30/sakura echo '<a href="' . $uri . '">Link</a>';

Quick Mapping

addMap() is a simple method to quick add route without complex options., (*17)

``` php $router->addMap('flower/(id)/sakura', array('_controller' => 'SakuraController', 'id' => 1));, (*18)

$variables = $router->match('flower/30/sakura');, (*19)


## Rules ### Simple Params ``` php new Route(null, 'flower/(id)-(alias)');

Optional Params

Single Optional Params

``` php new Route(null, 'flower(/id)');, (*20)


Matched route could be:

flower flower/25, (*21)


#### Multiple Optional Params ``` php new Route(null, 'flower(/year,month,day)');

Matched route could be:, (*22)

flower
flower/2014
flower/2014/10
flower/2014/10/12

The matched variables will be, (*23)

Array
(
    [year] => 2014
    [month] => 10
    [day] => 12
)

Wildcards

``` php // Match 'king/john/troilus/and/cressida' new Route(null, 'flower/(*tags)');, (*24)


Matched:

Array ( [tags] => Array ( [0] => john [1] => troilus [2] => and [3] => cressida ) ), (*25)


## Matchers Windwalker Router provides some matchers to use different way to match routes. ### Sequential Matcher Sequential Matcher use the [Sequential Search Method](http://en.wikipedia.org/wiki/Linear_search) to find route. It is the slowest matcher but much more customizable. It is the default matcher of Windwalker Router. ``` php use Windwalker\Router\Matcher\SequentialMatcher; $router = new Router(array(), new SequentialMatcher);

Binary Matcher

Binary Matcher use the Binary Search Algorithm to find route. This matcher is faster than SequentialMatcher but it will break the ordering of your routes. Binary search will re-sort all routes by pattern characters., (*26)

``` php use Windwalker\Router\Matcher\BinaryMatcher;, (*27)

$router = new Router(array(), new BinaryMatcher);, (*28)


### Trie Matcher Trie Matcher use the [Trie](http://en.wikipedia.org/wiki/Trie) tree to search route. This matcher is the fastest method of Windwalker Router, but the limit is that it need to use an simpler route pattern which is not as flexible as the other two matchers. ``` php use Windwalker\Router\Matcher\TrieMatcher; $router = new Router(array(), new TrieMatcher);

Rules of TrieMatcher

Simple Params

only match when the uri segments all exists. If you want to use optional segments, you must add two or more patterns., (*29)

flower
flower/:id
flower/:id/:alias

Wildcards

This pattern will convert segments after flower/ this to an array which named tags:, (*30)

flower/*tags

Single Action Router

Single action router is a simple router that extends Windwalker Router. It just return a string if matched., (*31)

This is a single action controller example:, (*32)

``` php $router->addMap('flower/(id)/(alias)', 'FlowerController');, (*33)

$controller = $router->match('flower/25/sakura');, (*34)

$_REQUEST = array_merge($_REQUEST, $router->getVariables());, (*35)

echo (new $controller)->execute();, (*36)


Or a controller with action name: ``` php $router->addMap('flower/(id)/(alias)', 'FlowerController::indexAction'); $matched = $router->match('flower/25/sakura'); $_REQUEST = array_merge($_REQUEST, $router->getVariables()); list($controller, $action) = explode('::', $matched); echo (new $controller)->$action();

RestRouter

RestRouter is a simple router extends to SingleActionRouter, it can add some suffix of different methods., (*37)

``` php $router->addMap('flower/(id)/(alias)', 'Flower\Controller\');, (*38)

$controller = $router->match('flower/25/sakura', 'POST'); // Get Flower\Controller\Create, (*39)

(new $controller)->execute();, (*40)


Default Suffix mapping is:

'GET' => 'Get', 'POST' => 'Create', 'PUT' => 'Update', 'PATCH' => 'Update', 'DELETE' => 'Delete', 'HEAD' => 'Head', 'OPTIONS' => 'Options', (*41)


You can override it: ``` php $router->setHttpMethodSuffix('POST', 'SaveController');

Exception

If Router not matched anything, it throws Windwalker\Router\Exception\RouteNotFoundException., (*42)

``` php try { $route = $router->match('flower/25'); } catch (RouteNotFoundException $e) { Application::close('Page not found', 404);, (*43)

exit();

} catch (\Exception $e) { // Do other actions... } ```, (*44)

The Versions

08/07 2018

dev-master

9999999-dev https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+ LGPL-2.0-or-later

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

08/07 2018

3.4.3

3.4.3.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0-or-later

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

08/07 2018

3.4.4

3.4.4.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0-or-later

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

15/06 2018

3.4.1

3.4.1.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0-or-later

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

15/06 2018

3.4.2

3.4.2.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0-or-later

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

20/02 2018

dev-test

dev-test https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+ LGPL-2.0-or-later

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

20/02 2018

3.3

3.3.0.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0-or-later

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

20/02 2018

3.3.1

3.3.1.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0-or-later

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

20/02 2018

3.3.2

3.3.2.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0-or-later

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

20/02 2018

3.4

3.4.0.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0-or-later

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

25/06 2017

3.2.4

3.2.4.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

25/06 2017

3.2.5

3.2.5.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

25/06 2017

3.2.6

3.2.6.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

25/06 2017

3.2.7

3.2.7.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

25/06 2017

3.2.8

3.2.8.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

09/06 2017

3.2.2

3.2.2.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

09/06 2017

3.2.3

3.2.3.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

05/06 2017

3.2.1

3.2.1.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

21/03 2017

3.2

3.2.0.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.5.9

 

The Development Requires

framework router windwalker

21/03 2017

3.1.4

3.1.4.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

21/03 2017

3.1.5

3.1.5.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

21/03 2017

3.1.6

3.1.6.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

11/10 2016

3.1

3.1.0.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

11/10 2016

3.1.1

3.1.1.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

11/10 2016

3.1.2

3.1.2.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

11/10 2016

3.1.3

3.1.3.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

18/07 2016

3.0

3.0.0.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

18/07 2016

3.0.1

3.0.1.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

04/07 2016

3.0-beta2

3.0.0.0-beta2 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

04/07 2016

3.0-beta

3.0.0.0-beta https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

21/03 2016

2.1.8

2.1.8.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

21/03 2016

2.1.9

2.1.9.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

12/02 2016

2.1.7

2.1.7.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

12/02 2016

2.1.6

2.1.6.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

07/09 2015

2.1.2

2.1.2.0 https://github.com/ventoviro/windwalker-language

Windwalker Language package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework language windwalker

16/08 2015

2.1.1

2.1.1.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

16/08 2015

2.1.4

2.1.4.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

16/08 2015

2.1.5

2.1.5.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

11/08 2015

2.1

2.1.0.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

27/07 2015

2.0.9

2.0.9.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

16/12 2014

2.0.0

2.0.0.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

16/12 2014

2.0.1

2.0.1.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

16/12 2014

2.0.2

2.0.2.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

16/12 2014

2.0.3

2.0.3.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

16/12 2014

2.0.4

2.0.4.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

16/12 2014

2.0.5

2.0.5.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

16/12 2014

2.0.6

2.0.6.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

16/12 2014

2.0.7

2.0.7.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

16/12 2014

2.0.8

2.0.8.0 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

24/11 2014

2.0.0-beta2

2.0.0.0-beta2 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

LGPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

05/10 2014

2.0.0-beta1

2.0.0.0-beta1 https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker

05/10 2014

2.0.0-alpha

2.0.0.0-alpha https://github.com/ventoviro/windwalker-router

Windwalker Router package

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.10

 

The Development Requires

framework router windwalker