PHP code example of sojf / routing
1. Go to this page and download the library: Download sojf/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/ */
sojf / routing example snippets
// 使用composer自动加载
Sojf\Routing\Compiler;
use Sojf\Routing\Compiled;
// 设置路由
$route = new Route('NORM:/api-(?<token>\w*)', 'Blog@user');
$compiler = new Compiler(); // 实例化编译器
$compiled = new Compiled(); // 实例化编译结果存储对象
$result = $compiler
->setRoute($route) // 设置要编译的路由
->setCompiled($compiled) // 设置编译结果存储类
->compile(); // 执行路由编译
print_r($result);
/*
Sojf\Routing\Compiled Object
(
[appName:protected] => blog
[routePath:protected] => /api-(?<token>\w*)
[routePathRegexp:protected] => ~^/api-(?<token>\w*)$~iu
[controllerMethod:protected] => user
[controller:protected] => app\Controller\Blog
[viewNameSpace:protected] => app\View
[modelNameSpace:protected] => app\Model
[routeType:protected] => NORM
[matchRes:protected] => Array
(
)
[hasCaptureVar] =>
)
*/
// 获取路由匹配正则表达式
$reg = $result->getRoutePathRegexp();
// 假设这是当前请求路由
$requestUlr = '/api-3bbe1210b';
// 执行匹配
preg_match($reg, $requestUlr, $matches);
if ($matches) {
echo '路由配置成功, 接着执行其它逻辑', '<br>', PHP_EOL;
print_r($matches);
/*
Array
(
[0] => /api-3bbe1210b
[token] => 3bbe1210b
[1] => 3bbe1210b
)
*/
} else {
echo '路由匹配失败';
}
// 使用composer自动加载
$route = new Sojf\Routing\Route('NORM:/user', 'UserController@detailsMethod', 'html|shtml', 'routeName');
// 使用composer自动加载
$compiler = new Sojf\Routing\Compiler();
// 实例话路由编译结果存储类
$compiled = new Sojf\Routing\Compiled();
// 实例化路由集合
$collection = new Sojf\Routing\Collection();
// 路由配置
$routes = array(
array('scheme' => 'NORM:/user', 'controller' => 'Blog@user'),
array('scheme' => 'NORM:/user', 'controller' => 'Blog@user'),
array('scheme' => 'NORM:/user', 'controller' => 'Blog@user')
);
// 批量添加路由到集合中
$collection->conf($routes);
// 假设这是当前请求URL
$requestUrl = '/user';
// 遍历路由集合,匹配当前请求URL
foreach ($collection as $name => $route) {
// 设置要编译的路由对象
$compiler->setRoute($route);
// 设置编译结果存储对象
$compiler->setCompiled($compiled);
// 开始编译路由对象
$compiled = $compiler->compile();
// 判断当前请求是否和路由匹配
if (!preg_match($compiled->getRoutePathRegexp(), $requestUrl, $matches)) {
// 没有匹配到路由,继续遍历路由集合
continue;
} else {
/*
* 匹配到当前请求对应的路由
* 保存匹配结果
* */
$compiled->setMatchRes($matches);
// 业务逻辑 。。
print_r($compiled);
break;
}
}
// 使用composer自动加载
Sojf\Routing\Compiler;
use Sojf\Routing\Compiled;
// 设置路由
$route = new Route('NORM:/api-(?<token>\w*)', 'Blog@user');
$compiler = new Compiler(); // 实例化编译器
$compiled = new Compiled(); // 实例化编译结果存储对象
$result = $compiler
->setRoute($route) // 设置要编译的路由
->setCompiled($compiled) // 设置编译结果存储类
->compile(); // 执行路由编译
print_r($result);
/*
Sojf\Routing\Compiled Object
(
[appName:protected] => blog
[routePath:protected] => /api-(?<token>\w*)
[routePathRegexp:protected] => ~^/api-(?<token>\w*)$~iu
[controllerMethod:protected] => user
[controller:protected] => app\Controller\Blog
[viewNameSpace:protected] => app\View
[modelNameSpace:protected] => app\Model
[routeType:protected] => NORM
[matchRes:protected] => Array
(
)
[hasCaptureVar] =>
)
*/
// 获取路由匹配正则表达式
$reg = $result->getRoutePathRegexp();
// 假设这是当前请求路由
$requestUlr = '/api-3bbe1210b';
// 执行匹配
preg_match($reg, $requestUlr, $matches);
if ($matches) {
echo '路由配置成功, 接着执行其它逻辑', '<br>', PHP_EOL;
print_r($matches);
/*
Array
(
[0] => /api-3bbe1210b
[token] => 3bbe1210b
[1] => 3bbe1210b
)
*/
} else {
echo '路由匹配失败';
}
php
composer