Skip to content

Commit a82b4dc

Browse files
committed
feat: fallback to default method
1 parent abe19f0 commit a82b4dc

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

system/Router/AutoRouterImproved.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Router;
1313

1414
use CodeIgniter\Exceptions\PageNotFoundException;
15+
use CodeIgniter\Router\Exceptions\MethodNotFoundException;
1516
use ReflectionClass;
1617
use ReflectionException;
1718

@@ -177,8 +178,21 @@ public function getRoute(string $uri): array
177178
// Check parameter count
178179
try {
179180
$this->checkParameters($uri);
180-
} catch (ReflectionException $e) {
181-
throw PageNotFoundException::forControllerNotFound($this->controller, $this->method);
181+
} catch (MethodNotFoundException $e) {
182+
// Fallback to the default method
183+
if (! isset($methodSegment)) {
184+
throw PageNotFoundException::forControllerNotFound($this->controller, $this->method);
185+
}
186+
187+
array_unshift($this->params, $methodSegment);
188+
$method = $this->method;
189+
$this->method = $this->defaultMethod;
190+
191+
try {
192+
$this->checkParameters($uri);
193+
} catch (MethodNotFoundException $e) {
194+
throw PageNotFoundException::forControllerNotFound($this->controller, $method);
195+
}
182196
}
183197

184198
return [$this->directory, $this->controller, $this->method, $this->params];
@@ -201,12 +215,21 @@ private function protectDefinedRoutes(): void
201215

202216
private function checkParameters(string $uri): void
203217
{
204-
$refClass = new ReflectionClass($this->controller);
205-
$refMethod = $refClass->getMethod($this->method);
206-
$refParams = $refMethod->getParameters();
218+
try {
219+
$refClass = new ReflectionClass($this->controller);
220+
} catch (ReflectionException $e) {
221+
throw PageNotFoundException::forControllerNotFound($this->controller, $this->method);
222+
}
223+
224+
try {
225+
$refMethod = $refClass->getMethod($this->method);
226+
$refParams = $refMethod->getParameters();
227+
} catch (ReflectionException $e) {
228+
throw new MethodNotFoundException();
229+
}
207230

208231
if (! $refMethod->isPublic()) {
209-
throw PageNotFoundException::forMethodNotFound($this->method);
232+
throw new MethodNotFoundException();
210233
}
211234

212235
if (count($refParams) < count($this->params)) {
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\Exceptions;
13+
14+
use RuntimeException;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class MethodNotFoundException extends RuntimeException
20+
{
21+
}

tests/system/Router/AutoRouterImprovedTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ public function testAutoRouteFindsDefaultDashFolder()
199199
$this->assertSame([], $params);
200200
}
201201

202+
public function testAutoRouteFallbackToDefaultMethod()
203+
{
204+
$router = $this->createNewAutoRouter();
205+
206+
[$directory, $controller, $method, $params]
207+
= $router->getRoute('index/15');
208+
209+
$this->assertNull($directory);
210+
$this->assertSame('\\' . Index::class, $controller);
211+
$this->assertSame('getIndex', $method);
212+
$this->assertSame(['15'], $params);
213+
}
214+
202215
public function testAutoRouteRejectsSingleDot()
203216
{
204217
$this->expectException(PageNotFoundException::class);

tests/system/Router/Controllers/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class Index extends Controller
1717
{
18-
public function getIndex()
18+
public function getIndex($p1 = '')
1919
{
2020
}
2121

0 commit comments

Comments
 (0)