PHP code example of next / routing

1. Go to this page and download the library: Download next/routing library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

next / routing example snippets


$router = new Router(array $options = [], ?Next\Routing\RouteCollection $routeCollection);

$router->get('/', function() {
    // Do something.
});

$router->request('/', function() {
    // Do something.
}, ['GET’, 'OPTIONS']);

$router->any('/', function() {
    // Do something.
});

// 带参数类型限制, 其中id只有为数字的时候会匹配到
$router->get('/book/{id:\d+}', 'BookController@show');
// 带后缀的路由,注意这里的符号.会被解析成正则元字符的一部分,因此有必要添加反斜线转义
$router->get('/p/{id}\.html', 'CateController@show');

$router->prefix('api')->group(function(\Next\Routing\Router $router) {
    $router->middleware('Authentication')->group(function(\Next\Routing\Router $router) {
        $router->get('/', function(\Next\Routing\Router $router) {
            // Do something.
        });
        
        $router->where('id', '\d+')->get('/user/{id}', 'UserController@show');
    });

$router->group(function(\Next\Routing\Router $router) {
    // 使用引入文件的方式
    

$routeCollection = $router->getRouteCollection();

$route = $routeCollection->resolve('GET', '/'); // 传递请求方式和path
$route = $routeCollection->resolveRequest($request); // 传递一个Psr\Http\Message\ServerRequestInterface对象进行解析