From 1d2e5f47d99cd0e04ff406309bb8a949de7ec2f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 22 Mar 2021 19:34:55 +0100 Subject: [PATCH 1/2] [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. --- src/Pecee/SimpleRouter/Router.php | 43 ++++++++++++++++++---- src/Pecee/SimpleRouter/SimpleRouter.php | 12 ++++++ tests/Pecee/SimpleRouter/RouterUrlTest.php | 42 ++++++++++++++++++++- 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index d9e642b..6f8781d 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -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; + } + } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index 284078b..2e79055 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -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 * diff --git a/tests/Pecee/SimpleRouter/RouterUrlTest.php b/tests/Pecee/SimpleRouter/RouterUrlTest.php index 2aa4706..8a1d1fa 100644 --- a/tests/Pecee/SimpleRouter/RouterUrlTest.php +++ b/tests/Pecee/SimpleRouter/RouterUrlTest.php @@ -11,7 +11,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase { TestRouter::get('/', 'DummyController@method1'); TestRouter::get('/page/{id?}', 'DummyController@method1'); - TestRouter::get('/test-output', function() { + TestRouter::get('/test-output', function () { return 'return value'; }); @@ -175,7 +175,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase { TestRouter::request()->setHost('google.com'); - TestRouter::get('/admin/', function() { + TestRouter::get('/admin/', function () { return 'match'; })->setMatch('/^\/admin\/?(.*)/i'); @@ -183,4 +183,42 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase $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); + } + } \ No newline at end of file From 5c8ff17aeca4b36c5d78d156f7546915c5b49ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 22 Mar 2021 19:43:13 +0100 Subject: [PATCH 2/2] Updated documentation for information about multi-route-rendering. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index f528de4..21dfc5a 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ You can donate any amount of your choice by [clicking here](https://www.paypal.c - [Registering new event](#registering-new-event) - [Custom EventHandlers](#custom-eventhandlers) - [Advanced](#advanced) + - [Disable multiple route rendering](#disable-multiple-route-rendering) - [Url rewriting](#url-rewriting) - [Changing current route](#changing-current-route) - [Bootmanager: loading routes dynamically](#bootmanager-loading-routes-dynamically) @@ -1444,6 +1445,12 @@ class DatabaseDebugHandler implements IEventHandler # 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 ### Changing current route