Skip to content

Commit 18b3db1

Browse files
committed
fix: directory is not correct
1 parent 283e744 commit 18b3db1

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

system/Router/AutoRouterImproved.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function __construct(
8787
string $httpVerb
8888
) {
8989
$this->protectedControllers = $protectedControllers;
90-
$this->namespace = rtrim($namespace, '\\') . '\\';
90+
$this->namespace = rtrim($namespace, '\\');
9191
$this->translateURIDashes = $translateURIDashes;
9292
$this->httpVerb = $httpVerb;
9393
$this->defaultController = $defaultController;
@@ -118,7 +118,7 @@ private function createSegments(string $uri)
118118
*/
119119
private function searchFirstController(array $segments): bool
120120
{
121-
$controller = '\\' . trim($this->namespace, '\\');
121+
$controller = '\\' . $this->namespace;
122122

123123
while ($segments !== []) {
124124
$segment = array_shift($segments);
@@ -160,7 +160,7 @@ private function searchLastDefaultController(array $segments): bool
160160
$segments
161161
);
162162

163-
$controller = '\\' . trim($this->namespace, '\\')
163+
$controller = '\\' . $this->namespace
164164
. '\\' . implode('\\', $namespaces)
165165
. '\\' . $this->defaultController;
166166

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

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

182182
if (class_exists($controller)) {
@@ -278,10 +278,14 @@ private function setDirectory()
278278

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

281-
$dir = substr($namespaces, strlen($this->namespace));
281+
$dir = str_replace(
282+
'\\',
283+
'/',
284+
ltrim(substr($namespaces, strlen($this->namespace)), '\\')
285+
);
282286

283287
if ($dir !== '') {
284-
$this->directory = substr($namespaces, strlen($this->namespace)) . '/';
288+
$this->directory = $dir . '/';
285289
}
286290
}
287291

tests/system/Router/AutoRouterImprovedTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ public function testAutoRouteFindsControllerWithSubfolder()
145145
$this->assertSame([], $params);
146146
}
147147

148+
public function testAutoRouteFindsControllerWithSubSubfolder()
149+
{
150+
$router = $this->createNewAutoRouter();
151+
152+
[$directory, $controller, $method, $params]
153+
= $router->getRoute('subfolder/sub/mycontroller/somemethod');
154+
155+
$this->assertSame('Subfolder/Sub/', $directory);
156+
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Sub\Mycontroller::class, $controller);
157+
$this->assertSame('getSomemethod', $method);
158+
$this->assertSame([], $params);
159+
}
160+
148161
public function testAutoRouteFindsDashedSubfolder()
149162
{
150163
$router = $this->createNewAutoRouter();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Router\Controllers\Subfolder\Sub;
13+
14+
use CodeIgniter\Controller;
15+
16+
class Mycontroller extends Controller
17+
{
18+
public function getSomemethod()
19+
{
20+
}
21+
}

0 commit comments

Comments
 (0)