PHP code example of xujiajun / tast-router
1. Go to this page and download the library: Download xujiajun/tast-router 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/ */
xujiajun / tast-router example snippets
//web/index.php
ute;
use TastRouter\Router;
use TastRouter\RouteCollection;
$collection = new RouteCollection();
$controller = 'TastRouter\\App\\Controllers\\UserController';
//普通用法
$collection
->attachRoute(new Route('/user/do',[
'_controller' => "$controller::doAction",
'methods' => 'GET',
]));
//使用正则
$collection
->attachRoute(new Route('/user/{user}',[
'_controller' => "$controller::indexAction",
'methods' => 'GET',
'user'=>'\w+',
// 'id'=>'\d+',
]));
//路由名绑定
$collection
->attachRoute(new Route('/hello/{hello}',[
'_controller' => "$controller::indexAction",
'methods' => 'GET',
'hello'=>'\w+',
'routeName'=>'say_hello',//bind route name
// 'id'=>'\d+',
]));
$router = new Router($collection);
$route = $router->matchCurrentRequest();
//解析路由
echo $router->generate('say_hello',['hello'=>'xujiajun']);// 输出 /hello/xujiajun
//web/index.php
outer;
use Symfony\Component\Yaml\Yaml;
$file = __DIR__.'/../src/Config/routes.yml';
$array = Yaml::parse(file_get_contents($file));
$router = Router::parseConfig($array);
$route = $router->matchCurrentRequest();