fastd / routing
FastD routing component
Installs: 18 970
Dependents: 5
Suggesters: 0
Security: 0
Stars: 16
Watchers: 2
Forks: 7
Open Issues: 1
Requires
- php: >=5.6
- fastd/http: ^3.0
- fastd/middleware: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5.0
- dev-master
- 5.0.x-dev
- 3.3.x-dev
- v3.2.6
- v3.2.5
- v3.2.4
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- 3.1.x-dev
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- 3.0.x-dev
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-rc2
- v3.0.0-rc1
- 2.0.x-dev
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- 2.0.0-rc1
- v2.0.0-beta2
- v2.0.0-beta1
- v0.1.2
- v0.1.1
- v0.1.0
- dev-develop
This package is auto-updated.
Last update: 2024-10-24 16:24:24 UTC
README
Simple PHP router that supports routing nesting, dynamic routing, fuzzy routing, middleware, and more. Relies on the http component.
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
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.
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
.
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.
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.
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.
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
Routing middleware
The routing component implements routing middleware based on [Http] (https://github.com/JanHuang/http) and [HTTP Middlewares] (https://github.com/JanHuang/middleware).
Routing middleware callbacks automatically call back
Psr\Http\Message\ServerRequestInterface
andFastD\Middleware\DelegateInterface
as arguments.
After the middleware call is completed, the \Psr\Http\Message\ResponseInterface
object is returned for the program to process the output.
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.
If you are happy with this, but don't know how to get started, you can try the following things:
- 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)