Navigation Menu

Skip to content

Commit

Permalink
Move Router::$initialized from RoutingMiddleware to `BaseApplicat…
Browse files Browse the repository at this point in the history
…ion`.

Refs #11357
  • Loading branch information
robertpustulka committed Oct 23, 2017
1 parent a073f43 commit 4498394
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/Http/BaseApplication.php
Expand Up @@ -17,6 +17,7 @@
use Cake\Core\ConsoleApplicationInterface;
use Cake\Core\HttpApplicationInterface;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

Expand Down Expand Up @@ -69,7 +70,11 @@ public function bootstrap()
*/
public function routes($routes)
{
require $this->configDir . '/routes.php';
if (!Router::$initialized) {
require $this->configDir . '/routes.php';
// Prevent routes from being loaded again
Router::$initialized = true;
}
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Routing/Middleware/RoutingMiddleware.php
Expand Up @@ -56,11 +56,9 @@ public function __construct(BaseApplication $app = null)
*/
protected function loadRoutes()
{
if ($this->app && !Router::$initialized) {
if ($this->app) {
$builder = Router::createRouteBuilder('/');
$this->app->routes($builder);
// Prevent routes from being loaded again
Router::$initialized = true;
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php
Expand Up @@ -152,7 +152,8 @@ public function testRoutesHookInvokedOnApp()
];
$this->assertEquals($expected, $req->getAttribute('params'));
$this->assertTrue(Router::$initialized, 'Router state should indicate routes loaded');
$this->assertCount(1, Router::routes());
$this->assertNotEmpty(Router::routes());
$this->assertEquals('/app/articles', Router::routes()[0]->template);
};
$app = new Application(CONFIG);
$middleware = new RoutingMiddleware($app);
Expand Down

0 comments on commit 4498394

Please sign in to comment.