mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 15:52:14 +00:00
Merge pull request #510 from skipperbent/v4-disable-multi-routing
[FEATURE] Option to disable multi-route-rendering #452
This commit is contained in:
@@ -82,6 +82,7 @@ You can donate any amount of your choice by [clicking here](https://www.paypal.c
|
|||||||
- [Registering new event](#registering-new-event)
|
- [Registering new event](#registering-new-event)
|
||||||
- [Custom EventHandlers](#custom-eventhandlers)
|
- [Custom EventHandlers](#custom-eventhandlers)
|
||||||
- [Advanced](#advanced)
|
- [Advanced](#advanced)
|
||||||
|
- [Disable multiple route rendering](#disable-multiple-route-rendering)
|
||||||
- [Url rewriting](#url-rewriting)
|
- [Url rewriting](#url-rewriting)
|
||||||
- [Changing current route](#changing-current-route)
|
- [Changing current route](#changing-current-route)
|
||||||
- [Bootmanager: loading routes dynamically](#bootmanager-loading-routes-dynamically)
|
- [Bootmanager: loading routes dynamically](#bootmanager-loading-routes-dynamically)
|
||||||
@@ -1362,6 +1363,12 @@ class DatabaseDebugHandler implements IEventHandler
|
|||||||
|
|
||||||
# Advanced
|
# Advanced
|
||||||
|
|
||||||
|
## Disable multiple route rendering
|
||||||
|
|
||||||
|
By default the router will try to execute all routes that matches a given url. To stop the router from executing any further routes any method can return a value.
|
||||||
|
|
||||||
|
This behavior can be easily disabled by setting `SimpleRouter::enableMultiRouteRendering(false)` in your `routes.php` file. This is the same behavior as version 3 and below.
|
||||||
|
|
||||||
## Url rewriting
|
## Url rewriting
|
||||||
|
|
||||||
### Changing current route
|
### Changing current route
|
||||||
|
|||||||
@@ -110,6 +110,13 @@ class Router
|
|||||||
*/
|
*/
|
||||||
protected $classLoader;
|
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.
|
* Router constructor.
|
||||||
*/
|
*/
|
||||||
@@ -399,15 +406,22 @@ class Router
|
|||||||
'route' => $route,
|
'route' => $route,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$output = $route->renderRoute($this->request, $this);
|
$routeOutput = $route->renderRoute($this->request, $this);
|
||||||
if ($output !== null) {
|
|
||||||
return $output;
|
if ($this->renderMultipleRoutes === true) {
|
||||||
|
if ($routeOutput !== null) {
|
||||||
|
return $routeOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = $this->handleRouteRewrite($key, $url);
|
$output = $this->handleRouteRewrite($key, $url);
|
||||||
if ($output !== null) {
|
if ($output !== null) {
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$output = $this->handleRouteRewrite($key, $url);
|
||||||
|
|
||||||
|
return $output ?? $routeOutput;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -912,4 +926,19 @@ class Router
|
|||||||
return $this->debugList;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -529,6 +529,18 @@ class SimpleRouter
|
|||||||
return $route;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set custom class-loader class used.
|
* Set custom class-loader class used.
|
||||||
* @param IClassLoader $classLoader
|
* @param IClassLoader $classLoader
|
||||||
|
|||||||
@@ -183,6 +183,44 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
|
|||||||
$this->assertEquals('match', $output);
|
$this->assertEquals('match', $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRenderMultipleRoutesDisabled()
|
||||||
|
{
|
||||||
|
TestRouter::router()->setRenderMultipleRoutes(false);
|
||||||
|
|
||||||
|
$result = false;
|
||||||
|
|
||||||
|
TestRouter::get('/', function () use (&$result) {
|
||||||
|
$result = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
TestRouter::get('/', function () use (&$result) {
|
||||||
|
$result = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
TestRouter::debug('/');
|
||||||
|
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRenderMultipleRoutesEnabled()
|
||||||
|
{
|
||||||
|
TestRouter::router()->setRenderMultipleRoutes(true);
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
|
||||||
|
TestRouter::get('/', function () use (&$result) {
|
||||||
|
$result[] = 'route1';
|
||||||
|
});
|
||||||
|
|
||||||
|
TestRouter::get('/', function () use (&$result) {
|
||||||
|
$result[] = 'route2';
|
||||||
|
});
|
||||||
|
|
||||||
|
TestRouter::debug('/');
|
||||||
|
|
||||||
|
$this->assertCount(2, $result);
|
||||||
|
}
|
||||||
|
|
||||||
public function testDefaultNamespace()
|
public function testDefaultNamespace()
|
||||||
{
|
{
|
||||||
TestRouter::setDefaultNamespace('\\Default\\Namespace');
|
TestRouter::setDefaultNamespace('\\Default\\Namespace');
|
||||||
|
|||||||
Reference in New Issue
Block a user