Skip to content

Commit

Permalink
Support different default controller names (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz authored Jan 6, 2024
1 parent 6248754 commit cbf6310
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/FileRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
];
Expand Down Expand Up @@ -131,7 +138,7 @@ private function parseRequestPath(ServerRequestInterface $request): iterable
$controllerName = $matches[2];
} else {
$directoryPath = $controllerName;
$controllerName = 'Index';
$controllerName = $this->defaultControllerName;
}

yield [
Expand Down
21 changes: 21 additions & 0 deletions tests/FileRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit cbf6310

Please sign in to comment.