Skip to content

Commit 4134a84

Browse files
committed
fix: spark routes shows invalid routes with Auto Routing Improved
Shows invalid routes with `x` to let devs know there are methods.
1 parent 7e0631c commit 4134a84

3 files changed

Lines changed: 80 additions & 3 deletions

File tree

system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ public function read(string $class, string $defaultController = 'Home', string $
117117
$params[$param->getName()] = $required;
118118
}
119119

120+
// If it is the default controller, the method will not be
121+
// routed.
122+
if ($classShortname === $defaultController) {
123+
$route = 'x ' . $route;
124+
}
125+
120126
$output[] = [
121127
'method' => $httpVerb,
122128
'route' => $route,

tests/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReaderTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;
1313

14+
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home;
1415
use CodeIgniter\Test\CIUnitTestCase;
1516
use Tests\Support\Controllers\Newautorouting;
1617
use Tests\Support\Controllers\Remap;
@@ -22,13 +23,13 @@
2223
*/
2324
final class ControllerMethodReaderTest extends CIUnitTestCase
2425
{
25-
private function createControllerMethodReader(): ControllerMethodReader
26-
{
26+
private function createControllerMethodReader(
27+
string $namespace = 'Tests\Support\Controllers'
28+
): ControllerMethodReader {
2729
$methods = [
2830
'get',
2931
'post',
3032
];
31-
$namespace = 'Tests\Support\Controllers';
3233

3334
return new ControllerMethodReader($namespace, $methods);
3435
}
@@ -63,6 +64,41 @@ public function testRead()
6364
$this->assertSame($expected, $routes);
6465
}
6566

67+
public function testReadDefaultController()
68+
{
69+
$reader = $this->createControllerMethodReader(
70+
'CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers'
71+
);
72+
73+
$routes = $reader->read(Home::class);
74+
75+
$expected = [
76+
0 => [
77+
'method' => 'get',
78+
'route' => '/',
79+
'route_params' => '',
80+
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home::getIndex',
81+
'params' => [],
82+
],
83+
[
84+
'method' => 'post',
85+
'route' => '/',
86+
'route_params' => '',
87+
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home::postIndex',
88+
'params' => [],
89+
],
90+
[
91+
'method' => 'get',
92+
'route' => 'x home/foo',
93+
'route_params' => '',
94+
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home::getFoo',
95+
'params' => [],
96+
],
97+
];
98+
99+
$this->assertSame($expected, $routes);
100+
}
101+
66102
public function testReadControllerWithRemap()
67103
{
68104
$reader = $this->createControllerMethodReader();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
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\Commands\Utilities\Routes\AutoRouterImproved\Controllers;
13+
14+
use CodeIgniter\Controller;
15+
16+
/**
17+
* The default controller for Auto Routing (Improved)
18+
*/
19+
class Home extends Controller
20+
{
21+
public function getIndex()
22+
{
23+
}
24+
25+
public function postIndex()
26+
{
27+
}
28+
29+
/**
30+
* This method cannot be accessible.
31+
*/
32+
public function getFoo()
33+
{
34+
}
35+
}

0 commit comments

Comments
 (0)