heropoo / routing by heropoo

A routing use "symfony/routing" and like "laravel/routing" style
Package Data
Maintainer Username: heropoo
Maintainer Contact: aiyouyou1000@163.com (Heropoo)
Package Create Date: 2017-08-08
Package Last Update: 2022-06-30
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2023-05-29 15:13:18
Package Statistics
Total Downloads: 5,575
Monthly Downloads: 2
Daily Downloads: 0
Total Stars: 10
Total Watchers: 1
Total Forks: 4
Total Open Issues: 0


A routing use "symfony/routing" and like "laravel/routing" style

To install it via composer

composer require heropoo/routing


require '../vendor/autoload.php';

use Moon\Routing\Router;

$router = new Router(null, [
    'namespace'=>'app\\controllers',    //support controller namespace
    'middleware'=>[                     //support middleware
    'prefix'=>''                        //support prefix

// action also can be a Closure
$router->get('/', function(){
    return 'Welcome \( ^▽^ )/';

//route parameter
$router->get('/hello/{name}', function($name){
    return 'Hello '.$name;
})->setRequirement('name', '([\w\s\x{4e00}-\x{9fa5}]+)?');  //  Perform a regular expression match

$router->get('/login', 'UserController::login')->name('login'); // name your route
$router->post('/login', 'UserController::post_login');

//use route group 
$router->group(['prefix'=>'user'], function($router){
     * @var Router $router
    $router->post('delete/{id}', 'UserController::delete');

// match GET or POST request method 
$router->match(['get', 'post'], '/api', 'ApiController::index');

// match all request method
$router->any('/other', 'ApiController::other');

echo '<pre>';

 * match request
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();

$path_info = $request->getPathInfo();

echo '<hr>'.$path_info.'<hr>';

$context = new \Symfony\Component\Routing\RequestContext();

$matcher = new \Symfony\Component\Routing\Matcher\UrlMatcher($routes, $context);
$parameters = $matcher->match($path_info);

//match request

Now use matched result to handle your controller's method or Closure! \( ^▽^ )/