diff --git a/README.md b/README.md index f528de4..c250220 100644 --- a/README.md +++ b/README.md @@ -1495,7 +1495,7 @@ class CustomRouterRules implement IRouterBootManager // If the current url matches the rewrite url, we use our custom route - if($request->getUrl()->getPath() === $url) { + if($request->getUrl()->contains($url)) { $request->setRewriteUrl($rule); } } diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 758204f..1b0f231 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -269,6 +269,13 @@ class Router { $this->debug('Loading routes'); + $this->fireEvents(EventHandler::EVENT_LOAD_ROUTES, [ + 'routes' => $this->routes, + ]); + + /* Loop through each route-request */ + $this->processRoutes($this->routes); + $this->fireEvents(EventHandler::EVENT_BOOT, [ 'bootmanagers' => $this->bootManagers, ]); @@ -291,13 +298,6 @@ class Router $this->debug('Finished rendering bootmanager "%s"', $className); } - $this->fireEvents(EventHandler::EVENT_LOAD_ROUTES, [ - 'routes' => $this->routes, - ]); - - /* Loop through each route-request */ - $this->processRoutes($this->routes); - $this->debug('Finished loading routes'); } diff --git a/tests/Pecee/SimpleRouter/BootManagerTest.php b/tests/Pecee/SimpleRouter/BootManagerTest.php new file mode 100644 index 0000000..925ea97 --- /dev/null +++ b/tests/Pecee/SimpleRouter/BootManagerTest.php @@ -0,0 +1,49 @@ + '/about', + '/contact' => '/', + ])); + + TestRouter::debug('/contact'); + + $this->assertTrue($result); + } + + public function testFindUrlFromBootManager() + { + TestRouter::get('/', 'DummyController@method1'); + TestRouter::get('/about', 'DummyController@method2')->name('about'); + TestRouter::get('/contact', 'DummyController@method3')->name('contact'); + + $result = false; + + // Add boot-manager + TestRouter::addBootManager(new FindUrlBootManager($result)); + + TestRouter::debug('/'); + + $this->assertTrue($result); + } + +} \ No newline at end of file diff --git a/tests/Pecee/SimpleRouter/Dummy/Managers/FindUrlBootManager.php b/tests/Pecee/SimpleRouter/Dummy/Managers/FindUrlBootManager.php new file mode 100644 index 0000000..f8bdcf3 --- /dev/null +++ b/tests/Pecee/SimpleRouter/Dummy/Managers/FindUrlBootManager.php @@ -0,0 +1,26 @@ +result = &$result; + } + + /** + * Called when router loads it's routes + * + * @param \Pecee\SimpleRouter\Router $router + * @param \Pecee\Http\Request $request + */ + public function boot(\Pecee\SimpleRouter\Router $router, \Pecee\Http\Request $request): void + { + $contact = $router->findRoute('contact'); + + if($contact !== null) { + $this->result = true; + } + } +} \ No newline at end of file diff --git a/tests/Pecee/SimpleRouter/Dummy/Managers/TestBootManager.php b/tests/Pecee/SimpleRouter/Dummy/Managers/TestBootManager.php index 8516aae..1617b46 100644 --- a/tests/Pecee/SimpleRouter/Dummy/Managers/TestBootManager.php +++ b/tests/Pecee/SimpleRouter/Dummy/Managers/TestBootManager.php @@ -3,13 +3,11 @@ class TestBootManager implements \Pecee\SimpleRouter\IRouterBootManager { - protected $routes; - protected $aliasUrl; + protected $rewrite; - public function __construct(array $routes, string $aliasUrl) + public function __construct(array $rewrite) { - $this->routes = $routes; - $this->aliasUrl = $aliasUrl; + $this->rewrite = $rewrite; } /** @@ -20,11 +18,11 @@ class TestBootManager implements \Pecee\SimpleRouter\IRouterBootManager */ public function boot(\Pecee\SimpleRouter\Router $router, \Pecee\Http\Request $request): void { - foreach ($this->routes as $url) { + foreach ($this->rewrite as $url => $rewrite) { // If the current url matches the rewrite url, we use our custom route if ($request->getUrl()->contains($url) === true) { - $request->setRewriteUrl($this->aliasUrl); + $request->setRewriteUrl($rewrite); } } diff --git a/tests/Pecee/SimpleRouter/EventHandlerTest.php b/tests/Pecee/SimpleRouter/EventHandlerTest.php index 8fa873d..faaa5d9 100644 --- a/tests/Pecee/SimpleRouter/EventHandlerTest.php +++ b/tests/Pecee/SimpleRouter/EventHandlerTest.php @@ -50,8 +50,8 @@ class EventHandlerTest extends \PHPUnit\Framework\TestCase // Add boot-manager TestRouter::addBootManager(new TestBootManager([ - '/test', - ], '/')); + '/test' => '/', + ])); // Start router TestRouter::debug('/non-existing'); @@ -61,7 +61,6 @@ class EventHandlerTest extends \PHPUnit\Framework\TestCase public function testAllEvent() { - $status = false; $eventHandler = new EventHandler();