doutuo7156 2016-09-01 15:35 采纳率: 100%
浏览 31

如何获得反向路由?

I have a rules in routing config

/**
 * Format
 * [controller, action($id,...), module]
 *
 * s: - string
 * i: - integer
 * {placeholder} - for replace in URL
 */
$rules = [
    '/' => ['site', 'index'],
    '/[s:action]' => ['site', '{action}'],
    '/[s:action]/[s:controller]' => ['{controller}', '{action}'],
    '/[s:module]/[s:controller]/[i:id]' => ['{controller}', 'view', '{module}'],
    '/[s:module]/[s:controller]/[s:action]/[i:id]' => ['{controller}', '{action}', '{module}'],
    '/[s:controller]/[i:id]/[i:id2]/[i:id3]' => ['{controller}', 'parse'],
];

How to get the reverse routing?

//Full URL => Rewrite URL 
$results = [
    '/' => '/',
    'site/index' => '/',
    'site/login' => '/login',
    'info/user' => '/user/info',
    'user/profile/view/123' => '/user/profile/123',
    'user/profile/edit/123' => '/user/profile/edit/123',
];

How to do it correctly for best performance?

Update This is routing code For parse pattern in previous step. As I understand to be treated as templates and routing parameters, to form the final path.

function checkRule($path, $pattern) {
    $params = [];

    if ($pattern === '*') {
        //Everyone
        $match = true;
    } elseif (isset($pattern[0]) && $pattern[0] === '@') {
        //Custom regexp
        $pattern = '`' . substr($pattern, 1) . '`u';
        $match = preg_match($pattern, $path, $params);
    } else {
        //Parse pattern
        $n = isset($pattern[0]) ? $pattern[0] : null;
        $route = null;
        $regex = false;
        $j = 0;
        $i = 0;

        // Find the longest non-regex substring and match it against the URI
        while (true) {
            if (!isset($pattern[$i])) {
                break;
            }

            if (false === $regex) {
                $c = $n;
                $regex = $c === '[' || $c === '(' || $c === '.';

                if (false === $regex && false !== isset($pattern[$i + 1])) {
                    $n = $pattern[$i + 1];
                    $regex = $n === '?' || $n === '+' || $n === '*' || $n === '{';
                }

                if (false === $regex && $c !== '/' && (!isset($path[$j]) || $c !== $path[$j])) {
                    return null;
                }

                $j++;
            }

            $route .= $pattern[$i++];
        }

        $regex = self::compileRoute($route);
        $match = preg_match($regex, $path, $params);
    }

    if ($match) {
        return $params;
    }

    return null;
}


function compileRoute($route) {
    if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
        $matchTypes = self::$matchTypes;

        foreach ($matches as $match) {
            list($block, $pre, $type, $param, $optional) = $match;

            if (isset($matchTypes[$type])) {
                $type = $matchTypes[$type];
            }

            if ($pre === '.') {
                $pre = '\.';
            }

            if ($param !== '') {
                $param = "?P<{$param}>";
            }

            if ($optional !== '') {
                $optional = '?';
            }

            $pattern = "(?:{$pre}({$param}{$type})){$optional}";
            $route = str_replace($block, $pattern, $route);
        }
    }

    return "`^{$route}$`u";
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
    • ¥15 C# datagridview 单元格显示进度及值
    • ¥15 thinkphp6配合social login单点登录问题
    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 虚心请教几个问题,小生先有礼了
    • ¥30 截图中的mathematics程序转换成matlab