From 31ade5474520bbc96d8be7a85241cf4c17b2c679 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 6 Jan 2024 18:30:11 +0700 Subject: [PATCH] Support different default controller names --- src/FileRouter.php | 21 ++++++++++++++------- tests/FileRouterTest.php | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/FileRouter.php b/src/FileRouter.php index b0f79c3..4de6356 100644 --- a/src/FileRouter.php +++ b/src/FileRouter.php @@ -12,9 +12,10 @@ final class FileRouter implements MiddlewareInterface { - public string $baseControllerDirectory = 'Controller'; - public string $classPostfix = 'Controller'; - public string $namespace = 'App'; + private string $baseControllerDirectory = 'Controller'; + private string $classPostfix = 'Controller'; + private string $namespace = 'App'; + private string $defaultControllerName = 'Index'; public function __construct( private readonly MiddlewareDispatcher $middlewareDispatcher, @@ -45,6 +46,14 @@ public function withNamespace(string $namespace): self return $new; } + public function withDefaultControllerName(string $name): self + { + $new = clone $this; + $new->defaultControllerName = $name; + + return $new; + } + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $possibleEntrypoints = $this->parseRequestPath($request); @@ -94,11 +103,9 @@ private function parseRequestPath(ServerRequestInterface $request): iterable $possibleAction = null; $path = urldecode($request->getUri()->getPath()); if ($path === '/') { - $controllerName = 'Index'; - yield [ $this->cleanClassname( - $this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $controllerName . $this->classPostfix + $this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $this->defaultControllerName . $this->classPostfix ), $possibleAction, ]; @@ -131,7 +138,7 @@ private function parseRequestPath(ServerRequestInterface $request): iterable $controllerName = $matches[2]; } else { $directoryPath = $controllerName; - $controllerName = 'Index'; + $controllerName = $this->defaultControllerName; } yield [ diff --git a/tests/FileRouterTest.php b/tests/FileRouterTest.php index 4b68ff2..c9fca9a 100644 --- a/tests/FileRouterTest.php +++ b/tests/FileRouterTest.php @@ -215,6 +215,27 @@ public function testUnusualControllerDirectory(): void $this->assertEquals('Hello, user/index action!', (string) $response->getBody()); } + public function testDefaultControllerName(): void + { + $router = $this->createRouter(); + $router = $router + ->withNamespace('Yiisoft\FileRouter\Tests\Support\App2') + ->withBaseControllerDirectory('Action') + ->withClassPostfix('Action') + ->withDefaultControllerName('User'); + + $handler = $this->createExceptionHandler(); + $request = new ServerRequest( + method: 'GET', + uri: '/', + ); + + $response = $router->process($request, $handler); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertEquals('Hello, user/index action!', (string) $response->getBody()); + } + public function testCustomRoute(): void { $router = $this->createRouter();