选择语言 :

 Core_Route::_matches

匹配路由

boolean|Ambigous Core_Route::_matches( string $uri , string $route_name )

参数列表

参数 类型 描述 默认值
$uri string 请求的URI
$route_name string 使用路由的名称
返回值
  • boolean|Ambigous <boolean, mixed, multitype:unknown >
File: ./core/classes/route.class.php
protected static function _matches($uri, $route_name)
{
    if (!$route_name || !(isset(Route::$regex[Core::$project][$route_name])))
    {
        return false;
    }

    if (IS_DEBUG)
    {
        Core::debug()->group('路由匹配');
        Core::debug()->info(array('URI:' => $uri, 'Route:' => Route::$regex[Core::$project][$route_name]),'匹配');
    }

    if (!preg_match(Route::$regex[Core::$project][$route_name], $uri, $matches))
    {
        if (IS_DEBUG)
        {
            Core::debug()->info('↑未匹配到当前路由');
            Core::debug()->groupEnd();
        }

        return false;
    }

    $params = array();
    foreach ($matches as $key => $value)
    {
        if (is_int($key))
        {
            // 跳过
            continue;
        }

        $params[$key] = $value;
    }

    $route_config = Core::config('route.'.$route_name);
    if ($route_config)
    {
        if (isset($route_config['default']) && is_array($route_config['default']))foreach($route_config['default'] as $key => $value)
        {
            if (!isset($params[$key]) || $params[$key] === '')
            {
                $params[$key] = $value;
            }
        }

        // 处理回调过滤
        if (isset($route_config['filters']) && is_array($route_config['filters']))foreach($route_config['filters'] as $callback)
        {
            $return = call_user_func($callback, $params);

            if (false===$return)
            {
                $params = false;
            }
            elseif (is_array($return))
            {
                $params = $return;
            }
        }
    }

    if (IS_DEBUG)
    {
        Core::debug()->info($params,'获取参数');
        Core::debug()->groupEnd();
    }

    return $params;
}