public function match($path, $params = [])
{
if (@preg_match($this->getPattern(), $path)) {
// check for site
if ($this->getSiteId()) {
if (!Site::isSiteRequest() || $this->getSiteId() != Site::getCurrentSite()->getId()) {
return false;
}
}
// we need to unset this 3 params here, because otherwise the defaults wouldn't have an effect if used
// in combination with dynamic action/controller/module configurations
unset($params["controller"], $params["action"], $params["module"]);
$params = array_merge($this->getDefaultsArray(), $params);
$variables = explode(",", $this->getVariables());
preg_match_all($this->getPattern(), $path, $matches);
if (is_array($matches) && count($matches) > 1) {
foreach ($matches as $index => $match) {
if (isset($variables[$index - 1]) && $variables[$index - 1]) {
$paramValue = urldecode($match[0]);
if (!empty($paramValue) || !array_key_exists($variables[$index - 1], $params)) {
$params[$variables[$index - 1]] = $paramValue;
}
}
}
}
$controller = $this->getController();
$action = $this->getAction();
$module = trim($this->getModule());
// check for dynamic controller / action / module
$dynamicRouteReplace = function ($item, $params) {
if (strpos($item, "%") !== false) {
uksort($params, function ($a, $b) {
// order by key length, longer key have priority
// (%abcd prior %ab, so that %ab doesn't replace %ab in [%ab]cd)
return strlen($b) - strlen($a);
});
foreach ($params as $key => $value) {
$dynKey = "%" . $key;
if (strpos($item, $dynKey) !== false) {
return str_replace($dynKey, $value, $item);
}
}
}
return $item;
};
$controller = $dynamicRouteReplace($controller, $params);
$action = $dynamicRouteReplace($action, $params);
$module = $dynamicRouteReplace($module, $params);
$params["controller"] = $controller;
$params["action"] = $action;
if (!empty($module)) {
$params["module"] = $module;
}
// remember for reverse assemble
$this->_values = $params;
return $params;
}
return [];
}