Skip to content

Commit

Permalink
fix: directory is not correct
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Apr 11, 2023
1 parent 283e744 commit 18b3db1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
16 changes: 10 additions & 6 deletions system/Router/AutoRouterImproved.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __construct(
string $httpVerb
) {
$this->protectedControllers = $protectedControllers;
$this->namespace = rtrim($namespace, '\\') . '\\';
$this->namespace = rtrim($namespace, '\\');
$this->translateURIDashes = $translateURIDashes;
$this->httpVerb = $httpVerb;
$this->defaultController = $defaultController;
Expand Down Expand Up @@ -118,7 +118,7 @@ private function createSegments(string $uri)
*/
private function searchFirstController(array $segments): bool
{
$controller = '\\' . trim($this->namespace, '\\');
$controller = '\\' . $this->namespace;

while ($segments !== []) {
$segment = array_shift($segments);
Expand Down Expand Up @@ -160,7 +160,7 @@ private function searchLastDefaultController(array $segments): bool
$segments
);

$controller = '\\' . trim($this->namespace, '\\')
$controller = '\\' . $this->namespace
. '\\' . implode('\\', $namespaces)
. '\\' . $this->defaultController;

Expand All @@ -176,7 +176,7 @@ private function searchLastDefaultController(array $segments): bool
}

// Check for the default controller in Controllers directory.
$controller = '\\' . trim($this->namespace, '\\')
$controller = '\\' . $this->namespace
. '\\' . $this->defaultController;

if (class_exists($controller)) {
Expand Down Expand Up @@ -278,10 +278,14 @@ private function setDirectory()

$namespaces = implode('\\', $segments);

$dir = substr($namespaces, strlen($this->namespace));
$dir = str_replace(
'\\',
'/',
ltrim(substr($namespaces, strlen($this->namespace)), '\\')
);

if ($dir !== '') {
$this->directory = substr($namespaces, strlen($this->namespace)) . '/';
$this->directory = $dir . '/';
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/system/Router/AutoRouterImprovedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ public function testAutoRouteFindsControllerWithSubfolder()
$this->assertSame([], $params);
}

public function testAutoRouteFindsControllerWithSubSubfolder()
{
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('subfolder/sub/mycontroller/somemethod');

$this->assertSame('Subfolder/Sub/', $directory);
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Sub\Mycontroller::class, $controller);
$this->assertSame('getSomemethod', $method);
$this->assertSame([], $params);
}

public function testAutoRouteFindsDashedSubfolder()
{
$router = $this->createNewAutoRouter();
Expand Down
21 changes: 21 additions & 0 deletions tests/system/Router/Controllers/Subfolder/Sub/Mycontroller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Router\Controllers\Subfolder\Sub;

use CodeIgniter\Controller;

class Mycontroller extends Controller
{
public function getSomemethod()
{
}
}

0 comments on commit 18b3db1

Please sign in to comment.