1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135:
<?php
namespace MvcCore\Router;
trait UrlByQuery {
public function UrlByQueryString ($controllerActionOrRouteName = 'Index:Index', array & $params = [], $givenRouteName = NULL) {
if ($givenRouteName == 'self') {
$params = array_merge($this->requestedParams ?: [], $params);
if (isset($params[static::URL_PARAM_PATH])) {
$defaultRouteName = static::DEFAULT_ROUTE_NAME;
if (
$controllerActionOrRouteName === $defaultRouteName || (
$this->currentRoute != NULL &&
$this->currentRoute->GetName() === $defaultRouteName
)
) unset($params[static::URL_PARAM_PATH]);
}
}
list($ctrlPc, $actionPc) = $this->urlByQueryStringCompleteCtrlAction(
$controllerActionOrRouteName, $params
);
$absolute = $this->urlGetAbsoluteParam($params);
$result = $this->urlByQueryStringCompleteResult(
$ctrlPc, $actionPc, $params
);
$result = $this->request->GetBasePath() . $result;
if ($absolute)
$result = $this->request->GetDomainUrl() . $result;
return $result;
}
protected function urlByQueryStringCompleteCtrlAction ($controllerActionOrRouteName, array & $params) {
list($ctrlPc, $actionPc) = strpos($controllerActionOrRouteName, ':') !== FALSE
? explode(':', $controllerActionOrRouteName)
: [NULL, NULL];
if (isset($params[static::URL_PARAM_CONTROLLER])) {
$ctrlPc = $params[static::URL_PARAM_CONTROLLER];
unset($params[static::URL_PARAM_CONTROLLER]);
}
if (isset($params[static::URL_PARAM_ACTION])) {
$actionPc = $params[static::URL_PARAM_ACTION];
unset($params[static::URL_PARAM_ACTION]);
}
$ctrlPc = str_replace('\\', '/', $ctrlPc);
return [$ctrlPc, $actionPc];
}
protected function urlByQueryStringCompleteResult ($ctrlPc, $actionPc, array & $params) {
$result = '';
$toolClass = self::$toolClass;
$amp = $this->getQueryStringParamsSepatator();
list($dfltCtrlPc, $dftlActionPc) = $this->application->GetDefaultControllerAndActionNames();
$ctrlIsNotDefault = $ctrlPc !== $dfltCtrlPc;
$actionIsNotDefault = $actionPc !== $dftlActionPc;
$sep = '?';
if ($params || $ctrlIsNotDefault || $actionIsNotDefault)
$result .= $this->request->GetScriptName();
if ($ctrlIsNotDefault) {
$result .= $sep . 'controller=' . $toolClass::GetDashedFromPascalCase($ctrlPc);
$sep = $amp;
}
if ($actionIsNotDefault) {
$result .= $sep . 'action=' . $toolClass::GetDashedFromPascalCase($actionPc);
$sep = $amp;
}
if ($params)
$result .= $sep . str_replace('%2F', '/', http_build_query($params, '', $amp, PHP_QUERY_RFC3986));
if ($result == '')
$result = '/';
return $result;
}
protected function urlGetAbsoluteParam (array & $params = []) {
$absolute = FALSE;
$absoluteParamName = static::URL_PARAM_ABSOLUTE;
if ($params && isset($params[$absoluteParamName])) {
$absolute = (bool) $params[$absoluteParamName];
unset($params[$absoluteParamName]);
}
return $absolute;
}
}