mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-15 18:23:26 +03:00
[FEATURE] Option to disable multi-route rendering
- Added option to disable multi-route rendering by calling `Router::setRenderMultipleRoutes($bool)`. - Added alias for easier access `SimpleRouter::enableMultiRouteRendering($bool)`. - Added php-unit tests for multi-routing enabled and disabled.
This commit is contained in:
@@ -110,6 +110,13 @@ class Router
|
||||
*/
|
||||
protected $classLoader;
|
||||
|
||||
/**
|
||||
* When enabled the router will render all routes that matches.
|
||||
* When disabled the router will stop execution when first route is found.
|
||||
* @var bool
|
||||
*/
|
||||
protected $renderMultipleRoutes = true;
|
||||
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
@@ -399,14 +406,21 @@ class Router
|
||||
'route' => $route,
|
||||
]);
|
||||
|
||||
$output = $route->renderRoute($this->request, $this);
|
||||
if ($output !== null) {
|
||||
return $output;
|
||||
}
|
||||
$routeOutput = $route->renderRoute($this->request, $this);
|
||||
|
||||
$output = $this->handleRouteRewrite($key, $url);
|
||||
if ($output !== null) {
|
||||
return $output;
|
||||
if ($this->renderMultipleRoutes === true) {
|
||||
if ($routeOutput !== null) {
|
||||
return $routeOutput;
|
||||
}
|
||||
|
||||
$output = $this->handleRouteRewrite($key, $url);
|
||||
if ($output !== null) {
|
||||
return $output;
|
||||
}
|
||||
} else {
|
||||
$output = $this->handleRouteRewrite($key, $url);
|
||||
|
||||
return $output ?? $routeOutput;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -912,4 +926,19 @@ class Router
|
||||
return $this->debugList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the rendering behavior of the router.
|
||||
* When enabled the router will render all routes that matches.
|
||||
* When disabled the router will stop rendering at the first route that matches.
|
||||
*
|
||||
* @param bool $bool
|
||||
* @return $this
|
||||
*/
|
||||
public function setRenderMultipleRoutes(bool $bool): self
|
||||
{
|
||||
$this->renderMultipleRoutes = $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -532,6 +532,18 @@ class SimpleRouter
|
||||
return $route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the rendering behavior of the router.
|
||||
* When enabled the router will render all routes that matches.
|
||||
* When disabled the router will stop rendering at the first route that matches.
|
||||
*
|
||||
* @param bool $bool
|
||||
*/
|
||||
public static function enableMultiRouteRendering(bool $bool): void
|
||||
{
|
||||
static::router()->setRenderMultipleRoutes($bool);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable dependency injection
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user