heropoo/routing

A simple and fast route. Use tree structure storage, fast matching!

v0.8.6 2022-06-30 17:42 UTC

README

A simple and fast route.Use tree structure storage, fast matching! If you like, order a star for me~ ⭐

一个简单快速的路由,使用树形结构存储匹配更快! 如果你喜欢,帮我点个star吧~ ⭐

中文说明

Latest Stable Version Total Downloads License

Install

To install it via composer

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! \( ^▽^ )/

Tests

composer run-script test

Sponsor

Jetbrains