PHP code example of cutephp / route

1. Go to this page and download the library: Download cutephp/route 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/ */

    

cutephp / route example snippets


use CutePHP\Route\Router;

$router = new Router;
//添加一个接受Get请求的路由
$router->get('/test', 'test');
//添加一个接受Post请求的路由
$router->post('/test', 'test');
//添加一个接受Delete请求的路由
$router->delete('/test', 'test');
//添加一个接受Put请求的路由
$router->put('/test', 'test');
//添加一个接受Head请求的路由
$router->head('/test', 'test');
//添加一个接受Patch请求的路由
$router->patch('/test', 'test');

use CutePHP\Route\Router;

$router = new Router;

$router->get('/about', '这是/about路由');
$router->get('/articles', '这是/articles路由');

//第一个参数为URI,第二个参数为HTTP方法。返回匹配的Route对象
$route = $router->match('/about','get');

//取出添加时第二个参数存储的值
echo $route->getStorage();

$router->add('/test',function(){
    return 123;
})->via('get','post');

$router->get('/test', 123, 'MyName');
$res = $route->name('MyName');

$res->getUri(); // '/test'
$res->getStorage(); // 123
$res->getMethods(); // array( 0 => 'GET')

$router->get('/test/:id',function(){
    return 123;
});

$res = $router->match('/test/2','get');
$params = $res->getParams();
var_dump($params);

$router->get('/users/:id?',function(){
    return 123;
});

array(1) {
  ["id"]=>
  string(1) "2"
}