2017 © Pedro Peláez
 

library routing

FastD routing component

image

fastd/routing

FastD routing component

  • Tuesday, July 17, 2018
  • by JanHuang
  • Repository
  • 1 Watchers
  • 9 Stars
  • 6,029 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 34 Versions
  • 10 % Grown

The README.md

FastD Routing

Build Status Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

Simple PHP router that supports routing nesting, dynamic routing, fuzzy routing, middleware, and more. Relies on the http component., (*2)

Claim

  • PHP 7.2

Composer

Composer require "fastd/routing"

Use

You can set the route through the RouteCollection object, or you can create a route through the route list. Detailed documentation: fastd/routing, (*3)

Static routing

use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/', function () {
    return 'hello world';
});

$route = $collection->match(new ServerRequest('GET', '/')); // \FastD\Routing\Route

echo call_user_func_array($route->getCallback(), []);

Route matching does not call the callback of the route, but returns the entire Route for callback processing, so match only returns the matching route object., (*4)

Dynamic routing

use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/{name}', function ($name) {
    return 'hello ' . $name;
});

$route = $collection->match(new ServerRequest('GET', '/foo')); // \FastD\Routing\Route

echo call_user_func_array($route->getCallback(), $route->getParameters());

Under dynamic routing, the successfully matched route will update the matching parameters to getParameters, and get the matching parameter information through getParameters., (*5)

Same route, multiple methods

$collection = new FastD\Routing\RouteCollection();
$collection->get('/', function () {
    return 'hello GET';
});
$collection->post('/', function () {
    return 'hello POST';
});
$response = $collection->dispatch('GET', '/'); // hello GET
$response = $collection->dispatch('POST', '/'); // hello POST

Hybrid routing

In many cases, our route may only be one parameter difference. Here is an example., (*6)

use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/{name}', function () {
    return 'get1';
});

$collection->addRoute('GET', '/', function () {
    return 'get2';
});

$route = $collection->match(new ServerRequest('GET', '/abc')); // \FastD\Routing\Route
$route2 = $collection->match(new ServerRequest('GET', '/')); // \FastD\Routing\Route
echo call_user_func_array($route->getCallback(), $route->getParameters());
echo call_user_func_array($route2->getCallback(), $route2->getParameters());

Routing Group

The routing group will add its own routing prefix to each of your sub-routing dollars, supporting multiple levels of nesting., (*7)

use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->group('/v1', function (RouteCollection $collection) {
    $collection->addRoute('GET', '/{name}', function () {
        return 'get1';
    });
});

$route = $collection->match(new ServerRequest('GET', '/v1/abc'));

echo call_user_func_array($route->getCallback(), $route->getParameters());

Fuzzy routing

The inspiration for fuzzy routing comes from the onRequest callback of the Swoole http server. Because each route entry passes onRequest, when it is created, there may be some special routes processed according to pathinfo. Then the fuzzy route can be sent. It’s time to use., (*8)

use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/api/*', function ($path) {
    return $path;
});

$route = $collection->match(new ServerRequest('GET', '/api/abc'));
echo call_user_func_array($route->getCallback(), $route->getParameters()); // /abc

$route = $collection->match(new ServerRequest('GET', '/api/cba'));
echo call_user_func_array($route->getCallback(), $route->getParameters()); // /cba

Match all legal routes that start with /api and then callback, (*9)

Routing middleware

The routing component implements routing middleware based on [Http] (https://github.com/JanHuang/http) and [HTTP Middlewares] (https://github.com/JanHuang/middleware)., (*10)

Routing middleware callbacks automatically call back Psr\Http\Message\ServerRequestInterface and FastD\Middleware\DelegateInterface as arguments., (*11)

After the middleware call is completed, the \Psr\Http\Message\ResponseInterface object is returned for the program to process the output., (*12)

use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/api/*', function (ServerRequest $request) {
    return 'hello';
});

$dispatcher = new \FastD\Routing\RouteDispatcher($collection);

$response = $dispatcher->dispatch(new ServerRequest('GET', '/api/abc'));

echo $response->getBody();

Testing

phpunit

Contribution

I am very pleased to be interested and willing to participate in the creation of a better PHP ecosystem, the developer of the Swoole Eco., (*13)

If you are happy with this, but don't know how to get started, you can try the following things:, (*14)

  • Problems encountered in your system [Feedback] (https://github.com/JanHuang/fastD/issues).
  • Have better suggestions? Feel free to contact [bboyjanhuang@gmail.com] (mailto:bboyjanhuang@gmail.com) or [Sina Weibo: Coding Man] (http://weibo.com/ecbboyjan).

Contact

If you encounter problems during use, please contact: bboyjanhuang@gmail.com. Weibo: [Coding Man] (http://weibo.com/ecbboyjan), (*15)

License MIT

The Versions

17/07 2018

dev-master

9999999-dev

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

17/07 2018

v3.2.2

3.2.2.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

10/07 2018

3.3.x-dev

3.3.9999999.9999999-dev

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

26/01 2018

dev-develop

dev-develop

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

26/01 2018

v3.2.1

3.2.1.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

23/01 2018

v3.2.0

3.2.0.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

11/01 2018

3.1.x-dev

3.1.9999999.9999999-dev

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

26/05 2017

v3.1.4

3.1.4.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

18/05 2017

v3.1.3

3.1.3.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

15/05 2017

v3.2.0-beta1

3.2.0.0-beta1

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

09/05 2017

v3.1.2

3.1.2.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

26/04 2017

v3.1.1

3.1.1.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

10/03 2017

v3.1.0

3.1.0.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

27/02 2017

3.0.x-dev

3.0.9999999.9999999-dev

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

17/02 2017

v3.0.5

3.0.5.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

06/02 2017

v3.0.4

3.0.4.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

03/02 2017

v3.0.3

3.0.3.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

01/02 2017

v3.0.2

3.0.2.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

29/01 2017

v3.0.1

3.0.1.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

17/01 2017

v3.0.0

3.0.0.0

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

15/01 2017

v3.0.0-rc2

3.0.0.0-RC2

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

10/01 2017

v3.0.0-rc1

3.0.0.0-RC1

FastD routing component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar JanHuang

routing

10/11 2016

2.0.x-dev

2.0.9999999.9999999-dev

FastD routing component

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Avatar JanHuang

routing

10/11 2016

v2.0.3

2.0.3.0

FastD routing component

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Avatar JanHuang

routing

10/08 2016

v2.0.2

2.0.2.0

FastD routing component

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Avatar JanHuang

routing

10/08 2016

v2.0.1

2.0.1.0

FastD routing component

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Avatar JanHuang

routing

19/06 2016

v2.0.0

2.0.0.0

FastD routing component

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Avatar JanHuang

routing

19/06 2016

2.0.0-rc1

2.0.0.0-RC1

FastD routing component

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Avatar JanHuang

routing

12/05 2016

v2.0.0-beta2

2.0.0.0-beta2

FastD routing component

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Avatar JanHuang

routing

01/05 2016

v2.0.0-beta1

2.0.0.0-beta1

FastD routing component

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Avatar JanHuang

routing

06/12 2015

dev-dev

dev-dev

FastD routing component

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Avatar JanHuang

route routing php7 router fastd framework fd

20/08 2015

v0.1.2

0.1.2.0

FastD routing component

  Sources   Download

MIT

The Requires

 

by Avatar JanHuang

route routing router fastd framework

17/07 2015

v0.1.1

0.1.1.0

FastD routing component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

route routing router fastd framework

20/06 2015

v0.1.0

0.1.0.0

FastD routing component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Avatar JanHuang

route routing router fastd framework