mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 19:02:19 +00:00
[FEATURE] getRoute now support domains.
- Optimisations + bugfixes.
This commit is contained in:
@@ -45,7 +45,6 @@ class RouterBase {
|
|||||||
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backStack = false, $group = null) {
|
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backStack = false, $group = null) {
|
||||||
// Loop through each route-request
|
// Loop through each route-request
|
||||||
|
|
||||||
$activeGroup = null;
|
|
||||||
$routesCount = count($routes);
|
$routesCount = count($routes);
|
||||||
$mergedSettings = array();
|
$mergedSettings = array();
|
||||||
|
|
||||||
@@ -54,6 +53,7 @@ class RouterBase {
|
|||||||
|
|
||||||
$route = $routes[$i];
|
$route = $routes[$i];
|
||||||
|
|
||||||
|
$route->addSettings($settings);
|
||||||
$route->setGroup($group);
|
$route->setGroup($group);
|
||||||
|
|
||||||
if($this->defaultNamespace && !$route->getNamespace()) {
|
if($this->defaultNamespace && !$route->getNamespace()) {
|
||||||
@@ -73,21 +73,20 @@ class RouterBase {
|
|||||||
array_push($newPrefixes, rtrim($route->getPrefix(), '/'));
|
array_push($newPrefixes, rtrim($route->getPrefix(), '/'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$route->addSettings($settings);
|
|
||||||
|
|
||||||
if(!($route instanceof RouterGroup)) {
|
if(!($route instanceof RouterGroup)) {
|
||||||
if(is_array($newPrefixes) && count($newPrefixes) && $backStack) {
|
if(is_array($newPrefixes) && count($newPrefixes) && $backStack) {
|
||||||
$route->setUrl( join('/', $newPrefixes) . $route->getUrl() );
|
$route->setUrl( join('/', $newPrefixes) . $route->getUrl() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$group = null;
|
||||||
$this->controllerUrlMap[] = $route;
|
$this->controllerUrlMap[] = $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->currentRoute = $route;
|
$this->currentRoute = $route;
|
||||||
|
|
||||||
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
|
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
|
||||||
|
$group = $route;
|
||||||
$route->renderRoute($this->request);
|
$route->renderRoute($this->request);
|
||||||
$activeGroup = $route;
|
|
||||||
$mergedSettings = array_merge($route->getMergeableSettings(), $settings);
|
$mergedSettings = array_merge($route->getMergeableSettings(), $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +97,7 @@ class RouterBase {
|
|||||||
$this->backStack = array();
|
$this->backStack = array();
|
||||||
|
|
||||||
// Route any routes added to the backstack
|
// Route any routes added to the backstack
|
||||||
$this->processRoutes($backStack, $mergedSettings, $newPrefixes, true, $activeGroup);
|
$this->processRoutes($backStack, $mergedSettings, $newPrefixes, true, $group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -239,7 +238,20 @@ class RouterBase {
|
|||||||
|
|
||||||
protected function processUrl($route, $method = null, $parameters = null, $getParams = null) {
|
protected function processUrl($route, $method = null, $parameters = null, $getParams = null) {
|
||||||
|
|
||||||
$url = '/' . trim($route->getUrl(), '/');
|
$domain = '';
|
||||||
|
|
||||||
|
if($route->getGroup() !== null && $route->getGroup()->getDomain() !== null) {
|
||||||
|
if(is_array($route->getGroup()->getDomain())) {
|
||||||
|
$domains = $route->getGroup()->getDomain();
|
||||||
|
$domain = array_shift($domains);
|
||||||
|
} else {
|
||||||
|
$domain = $route->getGroup()->getDomain();
|
||||||
|
}
|
||||||
|
|
||||||
|
$domain = '//' . $domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
$url = $domain . '/' . trim($route->getUrl(), '/');
|
||||||
|
|
||||||
if(($route instanceof RouterController || $route instanceof RouterResource) && $method !== null) {
|
if(($route instanceof RouterController || $route instanceof RouterResource) && $method !== null) {
|
||||||
$url .= $method;
|
$url .= $method;
|
||||||
@@ -251,20 +263,26 @@ class RouterBase {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* @var $route RouterEntry */
|
/* @var $route RouterEntry */
|
||||||
|
if(is_array($parameters)) {
|
||||||
|
$params = array_merge($route->getParameters(), $parameters);
|
||||||
|
} else {
|
||||||
$params = $route->getParameters();
|
$params = $route->getParameters();
|
||||||
if(count($params)) {
|
}
|
||||||
|
|
||||||
|
$otherParams = [];
|
||||||
|
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach($params as $param => $value) {
|
foreach($params as $param => $value) {
|
||||||
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
||||||
|
if(stripos($url, '{' . $param. '}') !== false || stripos($url, '{' . $param . '?}') !== false) {
|
||||||
$url = str_ireplace(array('{' . $param . '}', '{' . $param . '?}'), $value, $url);
|
$url = str_ireplace(array('{' . $param . '}', '{' . $param . '?}'), $value, $url);
|
||||||
|
} else {
|
||||||
|
$otherParams[$param] = $value;
|
||||||
|
}
|
||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// If no parameters are specified in the route, assume that the provided parameters should be used.
|
$url = rtrim($url, '/') . '/' . join('/', $otherParams);
|
||||||
if(count($parameters)) {
|
|
||||||
$url = rtrim($url, '/') . '/' . join('/', $parameters);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = rtrim($url, '/') . '/';
|
$url = rtrim($url, '/') . '/';
|
||||||
@@ -287,7 +305,7 @@ class RouterBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return current route if no options has been specified
|
// Return current route if no options has been specified
|
||||||
if($controller === null && $parameters === null && $this->loadedRoute !== null) {
|
if($controller === null && $parameters === null) {
|
||||||
$getParams = (is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET;
|
$getParams = (is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET;
|
||||||
|
|
||||||
$url = parse_url(Request::getInstance()->getUri());
|
$url = parse_url(Request::getInstance()->getUri());
|
||||||
|
|||||||
@@ -60,9 +60,7 @@ class RouterGroup extends RouterEntry {
|
|||||||
throw new RouterException('Method not allowed');
|
throw new RouterException('Method not allowed');
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->matchDomain($request) === null) {
|
$this->matchDomain($request);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return parent::renderRoute($request);
|
return parent::renderRoute($request);
|
||||||
}
|
}
|
||||||
@@ -80,4 +78,8 @@ class RouterGroup extends RouterEntry {
|
|||||||
return $this->exceptionHandler;
|
return $this->exceptionHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getDomain() {
|
||||||
|
return $this->domain;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ class RouterResource extends RouterEntry {
|
|||||||
|
|
||||||
$route = rtrim($this->url, '/') . '/{id?}/{action?}';
|
$route = rtrim($this->url, '/') . '/{id?}/{action?}';
|
||||||
|
|
||||||
$parameters = $this->parseParameters($route, $url);
|
$parameters = $this->parseParameters($route, $url, '[0-9]*?');
|
||||||
|
|
||||||
if($parameters !== null) {
|
if($parameters !== null) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user