Routing
A simple and fast route.Use tree structure storage, fast matching! If you like, order a star for me~ ⭐, (*1)
一个简单快速的路由,使用树形结构存储匹配更快! 如果你喜欢,帮我点个star吧~ ⭐, (*2)
中文说明, (*3)
, (*4)
Install
To install it via composer
, (*5)
composer require heropoo/routing
Feature
- Support restful style route
- Support route group and add some attributes (like namespace,middleware,prefix..)
- Support route params and limit param's type
- Support regex
- Use tree structure storage, fast matching!
Example:
<?php
require_once './vendor/autoload.php';
use Moon\Routing\Router;
use Moon\Routing\UrlMatchException;
$router = new Router([
'namespace'=>'app\\controllers', //support controller namespace
'middleware'=>[ //support middleware
'startSession',
'verifyCSRFToken',
'auth'
],
'prefix'=>'' //support prefix
]);
// action also can be a Closure
$router->get('/', function(){
return 'Welcome \( ^▽^ )/';
});
//route parameter
$router->get('/hello/{name}', function($name){ // auto pick route param to Closure
return 'Hello '.$name;
});
$router->get('/login', 'UserController::login', 'login'); // name your route
$router->post('login', 'UserController::post_login');
//use route group
$router->group(['prefix'=>'user'], function(Router $router){
$router->post('delete/{id:\d+}', 'UserController::delete'); // {param:type pattern}
});
// match GET or POST request method
$router->match(['get', 'post'], '/api', 'ApiController::index');
// match all request method
$router->any('/other', 'ApiController::other');
// get all routes
var_dump($router->getRoutes());
/**
* match request
* @param string $path Request path, eg: /user/list
* @param string $method Request method, 'GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS''GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'
* @return array If not matched throw a UrlMatchException
* return [
* 'route' => $route, // Route
* 'params' => $params // array
* ];
*
*/
$res = $router->dispatch($path, $method);
var_dump($res);
Now use matched result to handle your controller's method or Closure! \( ^▽^ )/, (*6)
Tests
composer run-script test
, (*7)